11

I'm just try to replace the content of a div with new content binded to a click event.

JSFIDDLE

When I use appendTo() instead of of html() it's working, but I need to clear the old content out so I'm a bit confused why this is not working like it should do

1
  • 1
    You should put some code to support your question along with jsfiddle link. Commented Dec 5, 2012 at 17:02

4 Answers 4

19

You are using content in place of selector and selector in place of content, you need to use # with id selctor. you can do it this way

Live Demo

function showitnow() {
    var div_data = "<div ><a href='XXX'>XXX</a></div>";
    $("#test1").html(div_data);

}

$("#button1").click(function() {
     showitnow();
});
​
Sign up to request clarification or add additional context in comments.

Comments

1

You have some mistakes in your fiddle, That should be like this: http://jsfiddle.net/pCckh/3/

function showitnow() {
    var div_data = "<div ><a href='XXX'>XXX</a></div>";
    $("#test1").html(div_data);
   //  ^---selector----^----------your content
}

$("#button1").click(function() {
    showitnow();
 // ^----------------remove the var and just call the function when click the button
});

Comments

0

maybe u can try this..
playing your JQuery as parent and child..

<div id="Destination"> 
    <div class="child_content"> item 1 </div>
    <div class="child_content"> item 2 </div>
</div>

    <div id="Source"> 
    <div class="child_content"> item 1 </div>
    <div class="child_content"> item 2 </div>
</div>

      <script type="text/javascript"> 
        $(document).ready(function(){
            $("#Source .child_content").click(function(){
                $(this).parent().remove();
                $(this).appendTo("#Destination");
            });
        });
    </script>

Hope this helpfull..

Comments

-1

Please have a look! Expected result...

Only few modification required in same example.

<div id = 'div_data'>
<button id="button1">BUTTON1</button>
div>

<div id="test2"></div>

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.