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 3 of 3
  • Thread Tools
  • Rate This Thread
  1. #1
    New Coder
    Join Date
    Feb 2015
    Posts
    38
    Thanks
    7
    Thanked 1 Time in 1 Post

    Clickable row in table works with only 1 param

    there are 2 versions of the cell2 call to function "show"
    Option A works. It calls with 1 parameter
    Option B does not. It calls with 2 parameters
    comment out one or the other of the options to see the problem
    (option B makes table but clicking does nothing)
    Code:
    <table id="myTable" border="1"></table>
    <script>
    var key='123';
    var display='hello';
    function show(key, display) {
    alert ('clicking worked')
    } //end show
    
     	var table = document.getElementById("myTable");
        var row = table.insertRow(0);
        var cell1 = row.insertCell(0);
     	var cell2 = row.insertCell(1);
    
    //what goes in cells:	
    	cell1.innerHTML = key
    
    // comment out only one of the 2 options
    	/*  OPTION A  */
    cell2.innerHTML =("<a onclick='show("+key+")'> "+display+" </a><br>"); // call with 1 param
    	
    	/* OPTION B   */
    //cell2.innerHTML =("<a onclick='show("+key+","+display+")'> "+display+" </a><br>");// call with 2 params
    
    </script>

  2. #2
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,123
    Thanks
    30
    Thanked 250 Times in 248 Posts
    /* OPTION B */
    //cell2.innerHTML =("<a onclick='show("+key+","+display+")'> "+display+" </a><br>");// call with 2 params
    "<a onclick='show("+key+",\""+display+"\")'> "+display+" </a><br>"
    because key is a number it doesn't need quotes
    display is a string so it does need quotes
    Last edited by DaveyErwin; 05-17-2016 at 12:11 AM.
    Cleverness is serviceable for
    everything,
    sufficient in nothing.

  3. #3
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,303
    Thanks
    30
    Thanked 864 Times in 862 Posts
    Lets take this a little farther. Your show function doesn't show anything except that the onclick has reached it. Better to use this as the function:
    Code:
    function show(keytest, displaytest) {
    	alert (keytest + " and " + displaytest);
    }
    Now we will know that we ran show() and what variables are passed to it.

    Because you are using single quotes, you do not need to escape for the variables. So the second onclick call can be:
    Code:
    cell2.innerHTML ="<a onclick='show(key, display)'>" + display + "</a><br>";
    Easier on the eyes and the coding brain, don't you think?

    If my putting ...test in there messes you up this also works:
    Code:
    cell2.innerHTML ="<a onclick='show(key, display)'>" + display + "</a><br>";
    // call with 2 params
    
    function show(key, display) {
    	alert (key + " and " + display);
    }
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.


 

Tags for this Thread

Posting Permissions

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