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 Coder
    Join Date
    Jun 2010
    Posts
    37
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Passing to a Function

    How do I pass an id to a function??
    Or is this even possible.

    Code:
    <html>
    <head>
    <title>
      JavaScript array passing example
    </title>
    <script type="text/javascript">
    
    function init(){
       display(addText1);
       display(addText2);
       display(addText3);
       display(addText4);
    }
    
    function display(var myText){
      var newText = '<h2>' + "Will this change anything..." + '</h2>';
    
       document.getElementById('myText').innerHTML = newText;
    }
    </script>
    </head>
    <body onload="init()">
    
    <div id="addText1"></div>
    <div id="addText2"></div>
    <div id="addText3"></div>
    <div id="addText4"></div>
    </body>
    </html>

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    The value of an HTML attribute is a Primitive: a string. Pass it as a string, then. And take care about how you use the quotes.
    Code:
    function init(){
       display("addText1");
       display("addText2");
       display("addText3");
       display("addText4");
    }
    function display(id){
      var newText = "<h2>Will this change anything...</h2>";
    
       document.getElementById(id).innerHTML = newText;
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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