RQST|:| If element's class contains "x" change HREF on click.

//_________________________\\ // ## :: _ x.1 _ :: ## \\ // If your classes are not subject to change dynamically (after the page loads) // then I suggest setting the "href" like this when the document loads, rather // than waiting for a user to click it. $( "#linkID[class~='foxtail']" ).attr( "href" , "http://www.xxx.com" ); //_________________________\\ // ## :: _ x.2 _ :: ## \\ // Alternatively, go with this route if you think your classes may be evolving // after being loaded. Although not a good practice (IMO) to attempt to change // an attribute the exact moment it is requested, it probably won't cause problems. $("#linkID").click(function() { // ^When the element is clicked. // Store classes in a string. // If your word is present, // then change the href. var linkClasses = $(this).attr("class"); if (linkClass.indexOf("foxtail") >= 0) { this.attr("href", "http://www.xxx.com"); } });
Request: "Hey!I was hoping to get some jquery/javascript help for a small script. I'll be testing this weekend but wondering if anyone had any further insight. Basically, I need to have a link check if a class has a certain word to redirect the link elsewhere. I have found a few tutorials but I haven't been able to do it myself quite yet or the descriptions in the documentation throws me off... Thanks in advance!"

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.