chibineku Posted April 30, 2009 Share Posted April 30, 2009 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 More sharing options...
jesh Posted April 30, 2009 Share Posted April 30, 2009 The jQuery selectors are similar to CSS.Check out the documentation:http://docs.jquery.com/SelectorsYou want to get at the click event of an element with an ID of "toggle"? Try this: $("#toggle").click(function(){}); Link to comment Share on other sites More sharing options...
chibineku Posted April 30, 2009 Author Share Posted April 30, 2009 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 More sharing options...
jesh Posted April 30, 2009 Share Posted April 30, 2009 $("a").toggleClass("test"); http://docs.jquery.com/Attributes/toggleClass#classEDIT: All together it would be something like this: $("#toggle").click(function(){ $("a").toggleClass("test");}); Link to comment Share on other sites More sharing options...
chibineku Posted April 30, 2009 Author Share Posted April 30, 2009 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 More sharing options...
jesh Posted April 30, 2009 Share Posted April 30, 2009 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 More sharing options...
chibineku Posted April 30, 2009 Author Share Posted April 30, 2009 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 More sharing options...
jesh Posted May 1, 2009 Share Posted May 1, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.