views:

61

answers:

5

What is the correct jquery statement to replace the "//Needed magic" comments below so that the image tags are hidden or unhidden based on the AJAX responses?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>JQuery</title>
<style type="text/css">
    .isSolvedImage{
        width: 68px;
        height: 47px;
        border: 1px solid red;
        cursor: pointer;
    }

</style>
<script src="_js/jquery-1.4.2.min.js" type="text/javascript"> </script>

</head>
<body>

<div id='true1.txt' class='isSolvedImage'>
    <img src="_images/solved.png">
</div>

<div id='false1.txt' class='isSolvedImage'>
    <img src="_images/solved.png">
</div>

<div id='true2.txt' class='isSolvedImage'>
    <img src="_images/solved.png">
</div>

<div id='false2.txt' class='isSolvedImage'>
    <img src="_images/solved.png">
</div>

<script type="text/javascript">

$(function(){
    var getDivs = 0;

    //iterate div with class isSolvedImage
    $("div.isSolvedImage").each(function() {
        alert('div id--'+this.id);
        // send ajax requrest
        $.get(this.id, function(data) {
            // check if ajax response is 1
            alert('div id--'+this.url+'--ajax response--'+data);            
            if(data == 1){
                alert('div id--'+this.url+'--Unhiding image--');
                //Needed magic
                //Show image if data==1 
            }
            else{
                alert('div id--'+this.url+'--Hiding image--');
                //Needed magic
                //Hide image if data!=1
            }
        });

      });
});
</script>
</body>
</html>
+1  A: 
$('#imageId').hide(); //to hide...
$('#imageId').show(); //to show...

or

$('img').hide(); //to hide all image...
$('img').show(); //to show all image...


$(function(){
    var getDivs = 0;

    //iterate div with class isSolvedImage
    $("div.isSolvedImage").each(function() {
        alert('div id--'+this.id);

        var img = $(this).find('img');

        // send ajax requrest
        $.get(this.id, function(data) {
            // check if ajax response is 1
            alert('div id--'+this.url+'--ajax response--'+data);            
            if(data == 1){
                alert('div id--'+this.url+'--Unhiding image--');
                //Needed magic
                //Show image if data==1 
                img.show();
            }
            else{
                alert('div id--'+this.url+'--Hiding image--');
                //Needed magic
                //Hide image if data!=1
                img.hide();
            }
        });

      });
});
Reigel
Hmmmmmmmmmmmmmm ... updated/copied again? :(
jAndy
what gives? huh??
Reigel
well whatsoever, nevermind. good answer
jAndy
well, if you are asking why I keep editing, it's because I'm not done yet... I just post it as the answer even if not done yet so that, I can have the time as early as possible.. ;) then keep editing until done.. ;)
Reigel
Thanks. Creating the var img before the ajax request made this very simple.
Chris
One more related question, if I return back the image url as data rather than returning a 1 or 0, how would I update the image url rather than hide/show it? img.update(...data...);
Chris
`img.attr('src', 'url-of-the-image.gif');` something like this...
Reigel
A: 
$('#true1.txt img').hide();

Where true1.txt is the ID of the div containing the image you wish to hide.

Greg B
I think you meant `$('#true1.txt img').hide();`, didn't you?
Igor Zinov'yev
@Igor: Yep, thanks!
Greg B
A: 

I think the easiest method is $('.target').hide(); and $('.target').show();

you can read more here :
.hide()
.show()
EDIT : I guess should have done refresh before answering.

Michael
+1  A: 
$(this).find('img').show();

or

$(this).find('img').hide();
jAndy
A: 

Hi!

$('div[id="' + $this.url + '"]').show();

and

$('div[id="' + $this.url + '"]').hide();

MAZ