Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    (AJAX => Div) don't work javascript in the div

    hello all

    i create one div and include by ajax (html code) but the problen, the jquery his don work into the div

    example:

    Code:
    <script src="http://code.jquery.com/jquery-1.3.js"></script>
    <style>
    div{
    	border:1px solid #000;
    	width:10%;
    	padding:15px;
    	margin: 5px;
    }
    </style>
    <a href='JAVASCRIPT:New_code_div()'>Click Here</a> for change code in div<br>
    <div id = 'id_div'>
    Original code<br>
    <a href='JAVASCRIPT:send_msg()'>msg from JS</a><br>
    <a href='#' class='msg'>msg from JQuery</a><br>
    </div>
    <script>
    function New_code_div(){
    	x = document.getElementById('id_div');
    	x.innerHTML = "include code<br><a href='JAVASCRIPT:send_msg()'>msg from JS</a><br><a href='#' class='msg'>msg from JQuery</a>";
    }
    function send_msg(){alert('hello from JS')}
    
    $(document).ready(function(){
    	$(".msg").click(function(){
    		alert("hello from JQuery");
    	});
    });
    </script>
    any solution for this problem ?????

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,395
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Yes

    The jQuery .click() handler can only bind to elements that already exist. You are adding class "msg" elements later but .click() is not bound to those elements.

    That's what .live('click', ...) is for. live will account for "future" elements, too.

  • #3
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •