Reputation: 174
hi is there any way i can find specific text within a value of an atrribute of an HTML tag, To be more specific, im trying to find out if the tag has "selected" within its "src" attribute e.g in this case i need to find out if selected exists, I can go in other routes to do this but i need this as a condition.
I am selecting the src of the img tag and adding this special text, but i dont want it to keep on adding e.g in this case its defeating the purpose of what im trying to do. I need to know if Selected has been inserted then ignore the particular image.
$(this).hover(function(){
var currentName = $(this).attr("src");
var theNumToSub = currentName.length - 4;
$(this).attr("src",$(this).attr("src").substr(0,theNumToSub)+"selected.jpg");
});
here is my code above for adding "Selected" in the first instance.
Upvotes: 0
Views: 64
Reputation: 3610
You can use indexOf()
if($(this).attr("src").indexOf('selected')>0){
//selected is there in src
//your stuff here
}
Upvotes: 0
Reputation: 4700
You can use the jQuery attribute contains selector :
$("img[src*='selected']");
Upvotes: 0
Reputation: 152304
You can try with:
$(this).filter('[src$="selected.jpg"]');
it returns the same element if src
ends with selected.jpg
.
You can also filter if anywhere in src
is selected
keyword with:
$(this).filter('[src*="selected"]');
Upvotes: 1