?
Solved

Access dynamically created table info

Posted on 2010-01-13
2
Medium Priority
?
322 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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

621 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