jquery addClass question

Discussion in 'Web Design and Development' started by olup, Apr 22, 2012.

  1. olup macrumors 6502

    Joined:
    Oct 11, 2011
    #1
    I'm slowly getting acquainted with javascript and jquery, and I set up an example for the .addClass function, which unfortunately doesn't work as I would like it to....I checked the jquery api section on it and pasted its example in my code to see, if/how it works. I then returned to my example, however it still doesn't seem to be working for whatever reason. Maybe you guys can help me out on this, would be much appreciated.
    Here's my code:

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    *{
    margin:0;
    padding:0;
    }
    .external {
    background:dodgerblue;
    }

    </style>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>

    <a href="http://www.macrumors.com">macrumors</a>

    <script>
    $("a[href^=http://]").addClass("external");
    </script>
    </body>
    </html>
     
  2. SCOLLAND macrumors newbie

    Joined:
    Jun 17, 2008
    #2
    You have no trigger for your addClass. There is nothing wrong with it, it is just never called.

    use:
    $(document).ready(function(){
    $(element).addClass('class');
    });

    If you want to run on page load.
     
  3. olup thread starter macrumors 6502

    Joined:
    Oct 11, 2011
    #3
    thanks for your help, scolland, unfortunately calling the function didn't solve the issue, which is driving me crazy. What boggles my mind is that the function also works without having to call it.
    http://api.jquery.com/addClass/
     
  4. elppa macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #4
    Change:
    Code:
    $("a[href^=http://]").addClass("external");
    to:
    Code:
    $("a[href^='http://']").addClass("external");
     
  5. olup thread starter macrumors 6502

    Joined:
    Oct 11, 2011
    #5
    @ elppa no. that didn't help either :(
    not sure why it's not working, since the code checks out ok

    edit: I finally figured it out....forgot to close the selector correctly....thanks for everyone's help!
     
  6. elppa macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #6

    You are the second person to tell me something doesn't work when it does.
    I do try this code before I paste it in! :)
     

Share This Page