Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If clicked img src contains specific text

Hi all how can i check If clicked img src contains specific text if then do some functions?

Example:

<img src="file:///C:/path/img4-dog.jpg">, <img src="file:///C:/path/img4-cat.jpg">

run alert if clicked img src contains 'dog' something like that?

like image 707
test Avatar asked Sep 13 '12 21:09

test


2 Answers

$('img').click(function(){
   if (this.src.indexOf("dog") != -1) 
      alert('contains dog');
})
like image 126
Claudio Redi Avatar answered Nov 12 '22 18:11

Claudio Redi


$('img').click(function(e) {

    if ($(this).attr('src').indexOf('dog') != -1) {
        alert('this contains dog');
    } 

});
like image 28
Gabriel Florit Avatar answered Nov 12 '22 17:11

Gabriel Florit