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
    5,034

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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
    5,034

    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
    Member
    Join Date
    Jul 2019
    Location
    Ahmedabad
    Posts
    57

    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
  •  



Click Here to Expand Forum to Full Width