www.webdeveloper.com
Results 1 to 5 of 5

Thread: Event Handling for dynamically created html element

  1. #1
    Join Date
    Mar 2011
    Posts
    3

    Event Handling for dynamically created html element

    Hi guys,
    Am new to javascript and am having this problem;

    //Some other code
    //Here I create an input element of type text and assign a onclick event property
    var quantityTxt = document.createElement("input");
    quantityTxt.type = "text";
    quantityTxt.onclick = calcAmount();
    quantityCell.appendChild(quantityTxt); //add the input field to a table cell
    //some other more code
    // The calcAmount function
    var table = document.getElementById('cashCollectionLineEntry'); the table
    var rowCount = table.rows.length;
    var quantity = 0;
    if(rowCount == 1){ //if the table has no other rows other than headers
    return;
    }else
    for (var i = 0; i < rowCount; i++) { //want to access the contents of //the table at this row on column 2
    quantity = table.rows[i].cells[2].childNodes[0].value;
    alert(i);

    }


    Now my problem is that the above function gets called even when the cell has not been clicked, hope am clear enough, any pointers would be highly appreciated.
    Thanks

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    No quite clear. Do you want: when click on an input to alert... what? The values of other input, or what? Which is your final aim?

  3. #3
    Join Date
    Mar 2011
    Posts
    3
    Thanks for the quick reply.
    Actually just forget the function (calcAmount), my issue is that the function is called whenever I call the function that creates the html input text element rather than wait for it to be clicked.

  4. #4
    Join Date
    Mar 2010
    Posts
    2,803
    Code:
     
    quantityTxt.onclick = calcAmount();
    should be

    Code:
     
    quantityTxt.onclick = calcAmount;
    You just want to reference the function when you assign it to the onclick and not actually run it at that stage.

  5. #5
    Join Date
    Mar 2011
    Posts
    3
    Thanks a lot that solves it, will find some time to read javascript concepts.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles