3

i am trying to change image in the div if the user click on any span,if div already contains roz variable(which comes from the index page) then by clickng on the span, img roz should not be appneded but it does not work.

i am not sure if the first part of IF statement in jQuery is right

here is html part

<div class="iconWrapper">
<ul class="color">
<li><a href="#" title="Selecteer"><span class="color1"></span></a> </li>
<li><a href="#" title="Selecteer "><span class="color2" ></span></a></li>
<li><a href="#" title="Selecteer"><span class="color3"></span></a> </l>
<li><a href="#" title="Selecteer"><span class="color4"></span></a></li>
</ul> </div>

<div id="div1" > <?php if(isset($_SESSION['img'])){
echo '<img src="' . $_SESSION['img'] . '" >' ; }
?>
<form method="post" action="weekDays.php">
<input name="kleur" type="text" value="" id="hiddencolor" />

<input name="submit" type="submit" value="Submit" />
</form> </div>

jQuery:

$(function(){
    var roz='../../photo/roz1.jpg';

    $('.iconWrapper span').click(function(e){
        var kleur=$(this).attr('class');
    if($('#div1').attr('src' == roz)){
        $('#div1' ).children('img').remove() ;
        $('#div1'). append('<img src="img/300.jpg" />');
    }else{
       $('#div1').children('img').remove();
       $('#div1').append('<img src="img/106.jpg" />');
    }
    e.preventDefault(); 
    alert(  $('#div1').html()  );       
});

});

2
  • 1
    What's your kleur variable for? You don't actually use it! Also, is #div1 actually an image tag? Because you've made it look like a div. Commented Jun 19, 2011 at 9:24
  • If you store the reference to $('#div1'). It will be faster. Commented Jun 19, 2011 at 9:26

4 Answers 4

6
if($('#div1').attr('src' == roz))

should be

if($('#div1').attr('src') == roz)
Sign up to request clarification or add additional context in comments.

Comments

2

I'm thinking you actually want this:

$(function() {
    $('.iconWrapper span').click(function(e) {
        $('#div1').find('img').attr('src', function(index, src) {
            return src == '../../photo/roz1.jpg' ? 'img/300.jpg' : 'img/106.jpg';
        });
    });
    e.preventDefault();
});

Given that your current code seems to be looking for a src attribute on what I presume is a div.

1 Comment

@Juhana: More specifically, it's the conditional operator
2

The jQuery attr() is being used incorrectly. You need to use attr('src') to get the 'src' attribute and then compare it to your roz value.

Comments

1

try this:

$(function() {
    var roz = '../../photo/roz1.jpg';
    $('.iconWrapper span').click(function(e) {
        var kleur = $(this).attr('class');
        if ($('#div1').attr('src') == roz) {
            $('#div1').children('img').remove();
            $('#div1').append('<img src="img/300.jpg" />');
        } else {
            $('#div1').children('img').remove();
            $('#div1').append('<img src="img/106.jpg" />');
        }
        e.preventDefault();
        alert($('#div1').html());
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.