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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    dynamic divs and Jquery

    Hey people of Codingforum.

    I've been lurking on here for an anwser but couldn't find a thread that could help me with this.

    I have .html file which loads in 3 .js files

    Code:
    <script src="./main/js/jquery.js" type="text/javascript"></script>
    <script src="./main/js/jquery.touch.js" type="text/javascript"></script>
    <script src="./main/js/site.js" type="text/javascript"></script>
    Jquery is needed for Jquery.touch (iphone/iPad library)
    site.js is a definition for div id's to check if touchable (depends on jquery.touch)

    Anyway if I manually add
    Code:
    <div id="touchme"><img class="largetouchbox" src="images/image.png"/></div>
    Then this div will be draggable and resizable on the iPad. (site.js basically sais that any div with the id "touchme" is draggable, rotateable and resizeable)

    But when I do this:

    Code:
    <head>
    <script type="text/javascript" language="javascript">
    
        function createDiv()
        {
            var divTag = document.createElement("div");
    		var div = document.getElementById("wrapper");   
    		     
            divTag.id = "touchme";
    		
    		divTag.innerHTML = "<img class='largetouchbox' src='images/burger.png' />";
            
            div.appendChild(divTag);
        }
    
        </script>
    </head>
    and

    Code:
    <body>
    <p align="center">
            <b>Click this button to create div element dynamically:</b>
            <input id="btn1" type="button" value="create div" onclick="createDiv();" />
        </p>
     
    </body>
    Then the button does add the div continuously, but the image is not draggable
    it's like the newly added divs aren't getting any javascript/jquery code on them.

    Is there a way to add this manually?

    thanks in advance

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    It's probably the age-old problem where the jQuery events do not know about the new divs. You should use the event .live() in your code, to make sure your jQuery events will apply to elements created in the future.

    Read this for the gritty detail:

    http://api.jquery.com/live/


  •  

    Posting Permissions

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