?
Solved

Access dynamically created table info

Posted on 2010-01-13
2
Medium Priority
?
295 Views
Last Modified: 2012-08-13
Hi -

I am building a jquery dynamic table from the code below. Everything works fine, but I need to access the data inside each row on a button click.

For example:
 If a user enters info into <input class="Field50 type="text" name="options[' + counter + '][sku]" />
I need to grab the info the user entered in the text field.

I tried:
    $('#optionGrid tr').each(function() {
        var customerId = $(this).find("td").eq(1).html();
        var optionssku = $("options[1][sku]").val();
        alert(optionssku)              
        });

But get "undefined" back (i think that means it can't find it...) and my guess is that it is that the table is getting created dynamically through an ajax call.

Thanks in advance



function buildTable(tableData) {
    var table = $("#optionGrid");
    table.empty()  //clear out the table if it was previously populated
    eval("var data = " + tableData);  //load the data variable as an object array
    table.append('<thead class="variationTable"><tr></tr></thead>');
    var thead = $('thead tr', table);  //create the table headers
    thead.append('<th>' + '<input type="checkbox" name="chkHeader" value="" />' + '</th>');
    for (var propertyName in $(data)[0]) {
        thead.append('<th>' + propertyName + '</th>');
    }    //add the table rows
    thead.append('<th>' + 'SKU' + '</th>');
    thead.append('<th>' + 'Price' + '</th>');
    thead.append('<th>' + 'Weight' + '</th>');
    thead.append('<th>' + 'Image' + '</th>');
    var counter = 0;
    $(data).each(function(key, val) {
        ++counter;
        table.append('<tr></tr>');
        var tr = $('tr:last', table);
        tr.append('<td><input type="checkbox" checked="checked" name="chk' + counter + '" value="' + counter + '" id="chk' + counter + '" /></td>');
        for (var propertyName in val) {
            tr.append('<td align="left" height="30px">' + val[propertyName] + '</td>');
            
        }
        tr.append('<td><input class="Field50 type="text" name="options[' + counter + '][sku]" /></td>');
        tr.append('<td><select class="PriceDrop" onchange="if(this.selectedIndex>0) { $(this).parent().find(\'span\').show(); $(this).parent().find(\'span input\').focus(); $(this).parent().find(\'span input\').select(); } else { $(this).parent().find(\'span\').hide(); } " name="options[' + counter + '][pricediff]"><option value="">-- No Change --</option><option value="add">Add</option><option value="subtract">Subtract</option><option value="fixed">Fixed</option></select><span style="display: none;">$<input class="Field50 PriceBox" type="text" value="" style="width: 40px;" name="price' + counter + '"/></span></td>');
        tr.append('<td><select class="WeightBox" onchange="if(this.selectedIndex>0) { $(this).parent().find(\'span\').show(); $(this).parent().find(\'span input\').focus(); $(this).parent().find(\'span input\').select(); } else { $(this).parent().find(\'span\').hide(); } " name="options[' + counter + '][weightdiff]"><option value="">-- No Change --</option><option value="add">Add</option><option value="subtract">Subtract</option><option value="fixed">Fixed</option></select><span style="display: none;"><input class="Field50 WeightBox" type="text" value="" style="width: 40px;" name="options[' + counter + '][weight]"/>LBS</span></td>');
        tr.append('<td> <div id="options' + counter + '"/><div id="optionsFilesUploaded' + counter + '"/></td>');
        
        $('#optionGrid td:nth-child(2),th:nth-child(2)').hide();
        VariationRowCreated(counter);
    });
            
     }

Open in new window

0
Comment
Question by:doctor069
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
Terry_focus earned 2000 total points
ID: 26302976
Hi,

This is tested as I don't have the data (maybe if you could attach the json) but should give you what you need.


//Correct this line
tr.append('<td><input class="Field50 type="text" name="options[' + counter + '][sku]" /></td>');
//To
tr.append('<td><input class="Field50" type="text" name="options[' + counter + '][sku]" /></td>');
        

$('#optionGrid tr').each(function(i) {
    var customerId = $(this).find("td").eq(1).html();
    var field = $(".Field50",this).val();//the field you we're looking for
    var selectPriceDrop = $(".PriceDrop",this).val();//The first select option
    alert("Feild50 Value:"+field+" Price drop select value:"+selectPriceDrop);
});

Open in new window

0
 

Author Closing Comment

by:doctor069
ID: 31676598
That did the trick!! Thanks Very Much!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question