#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Question Inject new div into the page


    Hello

    Code:
    var temp = 1;  
    
    document.getElementById('display').innerHTML +="<div id = \"temp\">  Here will be some image that will go to that div  </div>


    I'm using the above code to inject a new div that has some images in to the " display " div in the body of the page.

    Code:
    <div id = \"temp\"></div>
    the div id is set t a java script variable that changes. I tried the above and other syntax and it did not work.

    I want the div id name to increase every time this function is called.

    please help

    thanks.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,133
    Rep Power
    119
    You need to increment the "temp" variable; like so:

    Code:
    <script>
    var temp = 0;
    function injectDIV()
    {
    temp++;
    document.getElementById("display").innerHTML += "<div id = \""+temp+"\">  Here will be some image that will go to that div  </div>";
    }
    </script>
    
    <span id="display"></span>
    
    <input type="button" value="Inject New Content" onclick="injectDIV()"/>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    Thanks alot.

    I was missing to add the + variable +. The plus sign and this caused me lots of time.

    What is a good way of learning java script and all syntax
  6. #4
  7. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Originally Posted by yousg27
    Thanks alot.

    I was missing to add the + variable +. The plus sign and this caused me lots of time.

    What is a good way of learning java script and all syntax
    Most online JS tutorials seem to be out-dated - unless someone can recommend an up-to-date site?

    So I would recommend a book: JavaScript and jQuery: The Missing Manual, D S McFarland sounds good! (I haven't read it but his CSS, Missing Manual was very good.)

    JavaScript: The Definitive Guide is an excellent book, but not an introductory text. The Missing Manual will be an easier introduction/tutorial.
    "The mysql extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used." the docs

IMN logo majestic logo threadwatch logo seochat tools logo