dcsimg
Results 1 to 5 of 5

Thread: How to create and call a custom JQuery function?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,895

    How to create and call a custom JQuery function?

    I have a scenario in which I want create I need to set the display of several elements with the same class. My thinking was to create a custom jQuery function that contains a For Loop that would do this. I'm still learning jQuery and I've never created a custom function like you would in Javascript. The below code is what I need to put into this function. Should I just do this in Javascript?

    Code:
    var x = document.getElementsByClassName('reset');
    
    for (var i=0; i<x.length; i++) {
        x[i].style.display = 'none'
    }

    Thanks,
    Blake

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,319

    Re: How to create and call a custom JQuery function?

    You wouldn't use a loop in jQuery. You would use a selector. With a jQuery selector you can match zero, one or more elements and then the subsequent action is performed on each one. I suggest that you do some reading on jQuery selectors. Better you learn how to fish than just get handed this one fish. If you don't understand the principle, you'll be back looking for more fish in no time.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,319

    Re: How to create and call a custom JQuery function?

    By the way, jQuery is JavaScript. Its basically just a JavaScript library. Anything you write in jQuery, you are writing in JavaScript. It's a matter of whether you're using the functionality provided by the library or just the standard JavaScript functionality.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,895

    Re: How to create and call a custom JQuery function?

    I knew that it was Javascript...jQuery is just a wrapper from what I can tell. But I will definitely read up on it.

    Thanks jmc
    Blake

  5. #5
    Junior Member
    Join Date
    Jul 2019
    Location
    Ahmedabad
    Posts
    16

    Re: How to create and call a custom JQuery function?

    Code:
    <html>
    <head>
    <script>
    
    function get(){
    var x = document.getElementsByClassName('reset');
    
     for (var i=0; i<x.length; i++) {
        x[i].style.color = 'red'
      }
    }
    </script>
    </head>
    
    <body>
    <button onclick="get()">Click</button>
    
    <p class="reset">1</p>
    <p class="reset">2</p>
    <p>3</p>
    <p>4</p>
    
    </body>
    </html>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width