views:

231

answers:

3
<img style="cursor: pointer" src="../common/images/plus.jpg"/> View History($revisions)

How to select the image button above? I want to select image with a source of

../common/images/plus.jpg

and of course the image must be clickable.

I added

<script>
$("img[src=../common/images/plus.jpg]").click(function(){
    alert("ok");
});
</script>

However, I got an error

expected identifier or string for value in attribute selector but found '.'

+4  A: 

If you really want to select by src this should do it:

$('[src=../common/images/plus.jpg]')

... but you'd be better off giving it an ID - this is more reliable and faster.

<img id="imgPlus" style="cursor: pointer" src="../common/images/plus.jpg"/> View History($revisions)

then to select it you could just say:

$('#imgPlus')
Evgeny
$('[src=../common/images/plus.jpg]') doesn't work.
Steven
A slightly more flexible alternative would be $('[src$="common/images/plus.jpg"]'). That just matches the suffix of the src attribute rather than the whole thing.
Jesse Hallett
A: 

The easiest way is to provide the image with an id like

<img id="imgClick" style="cursor: pointer" src="../common/images/plus.jpg" />

and you can code like this

$("#imgClick").click(function(){
});
rahul
+2  A: 

Using jQuery you can use a CSS3 attribute selector to find the image:

$('img[src="../common/images/plus.jpg"]').click(function() {
  //handle click event here
});

Hope this helps!

devongovett
$('[src=../common/images/plus.jpg]') doesn't work.
Steven
Oops. You need to add quotes around the URL like so. $('img[src="../common/images/plus.jpg"]'). Check out this article for more information: http://forabeautifulweb.com/blog/about/image_management_naming_and_attribute_selectors/
devongovett