SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast revelstoke's Avatar
    Join Date
    Sep 2004
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is what I'm trying to do even possible?

    Why can't I pass in the name of the ID through the function so I can traverse the DOM looking for it and append to it where necessary?

    Code:
    function makeNotes(id) {
    	var theNote= prompt('Please enter note information', ' ');
    	
            var insertText = document.createTextNode(theNote);
    	var theBR = document.createElement('br');
    	
            document.getElementById(id).appendChild(insertText);
    	document.getElementById(id).appendChild(theBR);
    }
    Which gets called about 2 dozen times, each referencing a different block of notes (new element ID):

    Code:
    <a href="#" onClick="makeNotes('previousNotes')">Add Comments</a>
    But of course simply saying...

    Code:
    document.getElementById('previousNotes').appendChild(insertText);
    ...works perfectly. Anyone? Please help!

  2. #2
    SitePoint Enthusiast Tygatur's Avatar
    Join Date
    Apr 2006
    Location
    Germany
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tested your code and it works finde with IE FF Opera.
    Complete test file:
    HTML Code:
    <html>
     <head>
      <meta name="generator" content="HTML Studio" />
      <title> </title>
     </head>
     <body>
    <script type="text/javascript">
    function makeNotes(id) {
    var theNote= prompt('Please enter note information', ' ');
    
    var insertText = document.createTextNode(theNote);
    var theBR = document.createElement('br');
    
    document.getElementById(id).appendChild(insertText);
    document.getElementById(id).appendChild(theBR);
    }
    </script>
    <p id="tester"></p>
    <a href="javascript:makeNotes('tester')">tester</a>
     </body>
    </html>
    I haven't found the mistake you made but my version seems to work.
    to code or not to code ?
    that's too much of a question for a signature.

  3. #3
    SitePoint Enthusiast revelstoke's Avatar
    Join Date
    Sep 2004
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    odd

    Yea yours does work. Whats the deal? Yours and mine are identical, the only thing thats different is you chose a p tag to throw your ID into, I chose a div. The only other thing I can think of is maybe my doctype declaration is too strict?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    Other than that their identical really...

  4. #4
    SitePoint Enthusiast revelstoke's Avatar
    Join Date
    Sep 2004
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Solution:

    For some reason it preferred onClick="javascript:makeNotes()" better than just onClick="makeNotes()"

    go freaking figure.

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm, y ou have any url where you can show this?


Bookmarks

Posting Permissions

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