Jump to content

Jquery And Using Class/id As Selector


chibineku

Recommended Posts

I am just testing the jquery waters and I'm not sure how to go about selecting things by Id or class the way I could in simple JavaScript. For example:

$(document).ready(function() {$("button".id("toggle").click(function(event){if($("a").class="test") {$("a").test.removeClass("test") } else {$("a").addClass("test")}})

That looks really basic, but it doesn't work. Should I have put: if $("a.class")? I am not so sure about jquery, I think I'll take some convincing.

Link to comment
Share on other sites

Well, I was looking to test the class of all anchors, and if they have no class, add the class 'test', and if they already have that class, then remove it.

Link to comment
Share on other sites

Okay, I'm starting to come round to this jquery business, lol. That is stupidly easy.Muchos gracias :)

Link to comment
Share on other sites

No problem. It took me a long time to come around to it myself. I feel slightly dirty using it, but it's so incredibly easy. :)This in jQuery:

$("#toggle").click(function(){	$("a").toggleClass("test");});

would look something like this in regular java script:

var el = document.getElementById("toggle");if(el){	el.onclick = function()	{		var links = document.getElementsByTagName("a");		for(var i = 0; i < links.length; i++)		{			if(links[i].className.indexOf("test") > -1)			{				links[i].className = links[i].className.replace(/\btest\b/,"");			}			else			{				links[i].className += " test";			}		}	}}

Link to comment
Share on other sites

Hm...dirty is right, but sometimes dirty is good ;-) So are you quite well versed in jquery as well as JavaScript?

Link to comment
Share on other sites

I've only been using jQuery for a couple months now. I pretty much have to have the documentation open in one of my browser windows while I'm developing because I haven't memorized all of the functions yet.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...