Shruti Singh
Shruti Singh

Reputation: 71

How can hide Image control with expression in JQUERY

My question is that i have a image in a image control HTML now i need to hide that image. I have tried the code to hide the image as given below..

if ($("#ImageTest").attr(src="") == true) {
    $("#img").hide();
}
else {
    $("#img").show();
}

HTML code

<div id="ImageTest"> 
    <img id="img" src="C:\Image\MyImage\img.png"/> 
</div>

it is working fine when i provide an id to image control "img". but i need the code in which i am not providing the id to the image control. like >given below

<div id="ImageTest"> 
    <img src="C:\Image\MyImage\img.png"/> 
</div>

I need to search the control with expression like "src" and given image path, and after that i have to hide that image control.

Upvotes: 0

Views: 171

Answers (3)

Adil
Adil

Reputation: 148140

You can use Attribute Equals Selector [name="value"] to select images with empty src value and hide them. The images that have source not equal to empty string will be visible by default. You probably need to give the url instead of giving physical path of image.

$('img[src=""]').hide()

Your current condition also seem incorrect.

Change

if ($("#ImageTest").attr(src="") == true) {

To

if ($("#ImageTest").attr() == "" {

Upvotes: 2

paraselena
paraselena

Reputation: 261

you can use such code:

$('img[src="C:\Image\MyImage\img.png"]').hide()

Upvotes: 0

Anto Subash
Anto Subash

Reputation: 3215

you can select using the img tag and check for src try this

$('img').each(function(){
if ($(this).attr("src") == "") {
    $("#img").hide();
}
else {
    $("#img").show();
}
})

Upvotes: 1

Related Questions