SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Copy contents of element

    Hello! I've got a table where different rows are being created dynamically with input controls in each. For example:
    Row 1 contains txtCustomer1Name, txtAmount1Deposit, txtAmount1Available
    Row 2 contains txtCustomer2Name, txtAmount2Deposit, txtAmount2Available
    ...
    ...
    Row N contains txtCustomerNName, txtAmountNDeposit, txtAmountNAvailable


    What I would like to achieve is to copy the contents of Deposit to Available in real-time, i.e. as we type. And this should be done per row only.

    My piece of code looks like this:
    Code:
    $("input[name$='Deposit']").change(function() {
        alert("Deposit changed");
        $(this).next().val($(this).val());
    });
    However, it is not being triggered. Is it because the elements are dynamically being generated?

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,446
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Hi,

    Quote Originally Posted by girish3110 View Post
    My piece of code looks like this:
    Code:
    $("input[name$='Deposit']").change(function() {
        alert("Deposit changed");
        $(this).next().val($(this).val());
    });
    However, it is not being triggered. Is it because the elements are dynamically being generated?
    Yeah, absolutely right.. with dynamically added elements you need to delegate the event handler to a parent element, that way all new elements will also trigger the event:

    Code JavaScript:
    $("table#mytable").on("change",  "input[name$='Deposit']", function() {
        alert("Deposit changed");
        $(this).next().val($(this).val());
    });


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
  •