0

Am having the following code..

<img src="" id="image1" > <img src="www" id="image1" > <img src="" id="image2" > <img src="www" id="image2" > <div id="imagesrc"></div> 

How can I hide a div with an id (#imagesrc) when #image1 have src="", and show my div (#imagesrc) when #image1 have src !=""?

2
  • 3
    Your question is unclear because you have multiple elements with the same ID. The div #imagesrc cannot be shown and hidden at the same time. Or are these just examples, and there is only one element with the same ID and you just duplicated it to demonstrate that the element can have different values for src? Commented Mar 10, 2014 at 8:03
  • 1
    FWIW, if it is like I said, the shortest JS solution would be $('#imagesrc').toggle($('#image1[src!='']').length > 0). Commented Mar 10, 2014 at 8:06

6 Answers 6

2

CSS is clean, is JavaScript-free, and responds to changes automatically.

Of course, it assumes that your markup really looks like that.

#image1[src=""] ~ #imagesrc { display: none; } 
Sign up to request clarification or add additional context in comments.

Comments

1

try

 $(document).ready(function () { if ($("#image1").attr("src") == "") { $("#imagesrc").hide(); } else { $("#imagesrc").show(); } }); 

Update

$("#image1").change(function () { var imgControlName = "#imagesrc"; readURL(this, imgControlName); if (jQuery("#preview").attr("src") == "") { jQuery("#remove").hide(); } else { jQuery("#remove").show(); } }); 

Demo

4 Comments

Not work. When src =="" hidden my id, but when uploand my image and appear src data, not show my id.
how do you upload the image?
Here is my code. jsfiddle.net/J24yN/5. I want show remove button when src != "", else not show.
Button should disappear if I delete the picture.
0

try this

$(function(){ //ready function $('#imagesrc').show(); //show the imagesrc div.you can avoid this, if imagesrc div is already shown if($('#image1').attr('src')==""){ //check for condition $('#imagesrc').hide(); //if true hide it } }); 

Comments

0

Try this full code

you can use attr to call any html element attribute value or to set attribute value

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> </head> <body> <img src="" id="image1" > <img src="www" id="image1" > <img src="" id="image2" > <img src="www" id="image2" > <div id="imagesrc"></div> <script> $( document ).ready(function() { if ($("#image1").attr("src") == "") { $("#imagesrc").hide(); } else { $("#imagesrc").show(); } }); </script> </body> </html> 

3 Comments

Not work. When src =="" hidden my id, but when uploand my image and appear src data, not show my id.
what you mean of upload my image , are you using another code please post it here you need to mix your codes for final result
Here is my code. jsfiddle.net/J24yN/5. I want show remove button when src != "", else not show.
0

Try any one of the below approach.

 if($("#image1").attr('src') != ''){ $('#imagesrc').hide(); }else{ $('#imagesrc').show(); } 

or

$('#imagesrc').toggle($("#image1").attr('src') != ''); 

4 Comments

Not work. When src =="" hidden my id, but when uploand my image and appear src data, not show my id.
After uploading the image you should call the same code again to make the image visible
Here is my code. jsfiddle.net/J24yN/5. I want show remove button when src != "", else not show.
Upload (Add) a invalid file. if the image src is empty show remove button otherwise don't show. Hope this is what, you are trying to achieve.
0

//include jquery.js

<script type="text/javascript"> $(document).ready(function(){ if($('#image1').attr('src')==""){ $('#imagesrc').hide(); //you can also give $('#imagesrc').css('display','none'); } }); </script> 

Also please do not place same "id" to more than 1 element. Instead, use a class.

I hope you get an idea...

1 Comment

Not work. When src =="" hidden my id, but when uploand my image and appear src data, not show my id.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.