Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

olup

Cancelled
Original poster
Oct 11, 2011
383
40
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="https://www.macrumors.com">macrumors</a>

<script>
$("a[href^=http://]").addClass("external");
</script>
</body>
</html>
 
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.
 
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/
 
Change:
Code:
$("a[href^=http://]").addClass("external");

to:
Code:
$("a[href^='http://']").addClass("external");
 
@ 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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.