jQuery select anchor tags that contain specific text, then change id, onClick (Capitalized!!!!), href, :contains, .attr(), .each()
Wednesday, November 17th, 2010Easy actually, on load, select an anchor tag (or several, and do the following for .each()), that contains, among other contents, the word ‘Read’,
then set its id attribute, a Mouse event, and alter its href value:
$(document).ready(function(){
$(“a:contains(‘Read’)”).each(function(){
$(this).attr(‘id‘,’newid’);
$(this).attr(‘onClick‘, ‘return false;’); // Capitalize the C!!!!
$(this).attr(‘href‘, ‘http://www.alexyz.com’);
});
});
// alternatively, line them all up like this:
$(this).attr(‘id‘,’newid’).attr(‘onClick‘,’return false;’).attr(‘href‘,’http://www.alexyz.com’);
// UPDATE: Better method
$(“a[href*='search phrase']“).each(function(){
var postId = $(this).attr(“href”).split(‘?’)[1].split(‘=’)[1].split(‘#’)[0]; // of course this will vary by need
$(this).attr(“href“,”?version=full&source=folder&post=”+postId);
});