Peter Boomsma
Peter Boomsma

Reputation: 9808

How to target img src with jQuery

I have an hidden div with some menu items and a img src which is empty. A user can upload an image which will fill the img src and then I want to show the menu.

I've tried this, but I think this does not work because it does not check the img src.

$("#prev_img").change(function(){
  $("#menu").show();
});

I tried another way using the src lenght, but also no succes.

Upvotes: 0

Views: 2987

Answers (3)

Praxis Ashelin
Praxis Ashelin

Reputation: 5217

You can always retrieve the img source by

$("img").attr("src")

And implement any checks on that, for example:

if( $("img").attr("src") != "" ){
   $("#menu").show();
}

Upvotes: 0

dfsq
dfsq

Reputation: 193261

There is no onchange event for HTMLImageElement, but in your situation I think you can probably try to use onload event instead:

$("#prev_img").load(function() {
    $("#menu").show();
});

Demo: http://jsfiddle.net/hjk6r/

Upvotes: 1

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

You can get the image attribute, use this:

$("#prev_img").change(function(){
  $("#menu img[src='some_attr']").show();
});

Or you can try this:

$('#menu').find('img[src=some_attribute_value_for_source').show();

http://api.jquery.com/has-attribute-selector/ jQuery Attribute Selector

https://api.jquery.com/attribute-equals-selector/ jQuery Attribute value selector

$("[attribute='value']")

Selects and element with attribute of the value.

Upvotes: 0

Related Questions