i am trying to remove the div old image and append a new one using last child(jQuery) but it does not work i get a new image appended beside the old one.
here is jQuery code
$(function() {
$('.iconWrapper span').click(function(e){
var kleur=$(this).attr('class');
switch(kleur){
case 'color1':
$("#div1 img:last-child").remove();
alert($("#div1").html());
$('#div1').append('<img src="color/color1.jpg">');
break;
case 'color2':
$("#div1 img:last-child").remove();
$('#div1').append('<img src="color/color2.jpg">');
break;
case 'color3':
$("#div1 img:last-child").remove();
$('#div1').append('<img src="color/color3.jpg">');
break;
}
$('#hiddenimg').val(kleur);
e.preventDefault();
});
html code::
<?php
error_reporting('E_ALL');
ini_set('display_errors', 1);
session_start();
$img=$_POST['img'];
$_SESSION['img']=$img;
?>
<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>
</li>
<li>
<a href="#" title="Selecteer"><span class="color4"></span></a>
</li>
</ul>
</div>
<form method="post" action="step3.php">
<div id="div1" >
<?php if(isset($_SESSION['img'])){
echo '<img src="' . $_SESSION['img'] . '" >' ;
} ?>
<input name="color" type="text" value="" id="hiddenimg" />
<input name="submit" type="submit" value="Submit" />
</div>
</form>
you can try the jQuery :last selector: link
for you need something like:
or a better version
I don’t know to test right now, I hope this is your working solution 🙂
(Sorry for my english :”> )