0

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.

2
  • It would probably be a good idea to familiarise yourself with all of the jQuery selectors. Commented Jan 27, 2014 at 12:42
  • Can you show how src looks like. The actual string in src? Commented Jan 27, 2014 at 12:43

3 Answers 3

1

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"]');
Sign up to request clarification or add additional context in comments.

2 Comments

ended up using this method
@Wandile If you find my answer helpful, remember to accept it with the tick on the left.
0

You can use the jQuery attribute contains selector :

$("img[src*='selected']");

1 Comment

this looks good but remember Florian im trying to run away from using the element with this text in it
0

You can use indexOf()

if($(this).attr("src").indexOf('selected')>0){
  //selected is there in src
  //your stuff here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.