70877

Why Remove last child does not work?

Question:

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>

Answer1:

you can try the jQuery :last selector: <a href="http://api.jquery.com/last-selector/" rel="nofollow">link</a>

for you need something like:

$("#div1 img:last").remove();

or a better version

$("#div1 img").filter(":last").remove();

I don't know to test right now, I hope this is your working solution :)

(Sorry for my english :"> )

Answer2:

You need :last and not :last-child

Recommend

  • How to position pernament div in custom place on page
  • HTTP GET method parameter format for object
  • Cannot set property 'innerHTML' of null while still using window.onload to delay
  • LLVM 3.0 compiler error: cast of C pointer type to Objective-C pointer type 'id' requires
  • Avoid jQuery UI resizable 'alsoResize' to move in opposite direction
  • ONLY Visible Div form elements will get submitted
  • $_POST for text in DIV elements
  • Is it better to use the “hidden” CSS attribute or fetch each set of new images?
  • Python PIL to extract number from image
  • Outputting SharePoint Hyperlink Column as URL
  • Disable Kendo Autocomplete
  • Remove changes from one element when event occurs on another element?
  • How to view images from protected folder with php?
  • How to use JavaScript to determine whether a file exists in a directory?
  • Display images in Django
  • Functions in global context
  • Google Custom Search with transparent background
  • Android fill_parent issue
  • Highlight and Bold text in JTextPane
  • Algorithm for a smudge tool?
  • FileReader+canvas image loading problem
  • How reduce the height of an mschart by breaking up the y-axis
  • Resize panoramic image to fixed size
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • JSON with duplicate key names losing information when parsed
  • Importing jscolor library in angular 2
  • How to make Safari send if-modified-since header?
  • How to pass list parameters for each object using Spring MVC?
  • Compare two NSDates in iPhone
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Android Google Maps API OnLocationChanged only called once
  • JaxB to read class hierarchy
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal