0

I want to take img.fnone and replace it under span.ddlabel element via JavaScript or jquery is there any function to do that

  <span id="flat_title" class="ddTitleText ">
    <img class="fnone" src="images/pinc.png">
    <span class="ddlabel">A</span>
    <span class="description" style="display: none;"></span>
    </span>

4 Answers 4

3

Use append()

var targetSPAN=$('#flat_title');
var image=$('img.fnone', targetSPAN);

$('span.ddlabel', targetSPAN).append(image);

JSFIDDLE DEMO

3
  • Why bother to clone and remove? If you append it to somewhere else it simply moves, it's not a copy.
    – Nunners
    Nov 28, 2013 at 12:21
  • yes it's working fine, but what if there are many elements with the same class names in the page and i want to do the same action
    – JS-Hero
    Nov 28, 2013 at 12:49
  • @thehero75 For that you would have to use an ID, or loop over each of the elements and find the correct item you want to move it to.
    – Nunners
    Nov 29, 2013 at 8:34
0

<script type="text/javascript">
    $(document).ready(function () {

        $('span .ddlabel').append('<img class="fnone" src="images/pinc.png">');
        $('#flat_title img:first').remove()

    });

</script>
0

Make use of element.appendChild method.

<span id="flat_title" class="ddTitleText ">
    <img class="fnone" src="images/pinc.png" />
    <span class="ddlabel">A</span>
    <span class="description" style="display: none;"></span>
</span>
<div>
    <button id="abc">Click to replace</button>
</div>
<script>
document.getElementById('abc').addEventListener('click',function(){
    var parent = document.getElementById('flat_title'),
    img = document.getElementsByClassName('fnone')[0],
    description = document.getElementsByClassName('description')[0];

    parent.appendChild(img);
    parent.appendChild(description);
})
</script>

Check it on http://jsfiddle.net/fWK67/

0

Use html()

var html=$("img.fnone").html();
$("span.ddlabel").html(html);
$("img.fnone").remove();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.