Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript document.write to a specific id

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #42727

    I’ve been trying to make a function which makes a new div when clicked.

    I use this code:


    <script type="text/javascript">
    function printYTVideo(fileUrl) {
    var ytDiv = document.getElementById('ytgoeshere');
    ytDiv.appendChild(document.write('<div id="imjusttesting"><div id="youtube"><iframe width="640" height="360" src="' + fileUrl + '" frameborder="0" allowfullscreen></iframe></div></div>'));
    }
    </script>

    …and this code for activating it:


    <li onclick="printYTVideo('http://www.youtube.com/embed/9cIjg2BibAM?feature=player_detailpage')">Trailer</li>

    But when I activate it, it doesn’t add that div, but it deletes the entire page content and prints the div. Any ideas? Thank you all!

    // Simon

    EDIT: Sorry for messy code

    #124577
    Paulie_D
    Member

    Can you not use ‘insertAfter’ instead of ‘append’?

    #124578

    Paulie_D, Could you show me an example? :)

    #124586
    Paulie_D
    Member

    Here’s one using jQuery in document.ready mode. http://codepen.io/anon/pen/vFcuh

    Two functions:

    One using ‘insertAfter’ targeting putting something after a specific ID and the second ‘appendTo’ putting it after the content of the ‘article’.

    Please note that I am VERY new to JQ and I’m just getting into it.

    Certainly you could create a function and assign that function to a click event…but I’m still learning.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.