1

I am trying to store data into an associative array from table. Here is my html table code:

<table class="table" border="1" id="eidtPersonalInfoTbl">         
    <tr>    
        <td class="span3 hidden-phone">Name</td>    
        <td class="span5"> Name of Doctor </td>
    </tr>    
    <tr> 
        <td class="span3 hidden-phone">Address</td>
        <td class="span5"> Address of doctor </td>
    </tr>
    <tr>
        <td class="span3 hidden-phone">Area</td>
        <td class="span5"> Area</td>
    <tr>
 <table>

and I am trying to access table values by this code:

<script type="text/javascript">
    $(document).on('click', '.editPersonalInfo', function() {
        modal();
        alert(obj['Name']);
        alert(obj['Address']);
        alert(obj['Area']);
    });

    function modal() {
        var table = $("#eidtPersonalInfoTbl");
        var trs = table.find('tr');
        var obj = {};
        $(trs).each(function(index, row) {
            var field = $(row).find('td').eq(0).text();
            var value = $(row).find('td').eq(1).text();
            obj[field] = [value];
        });
    }
</script>

but it is not working. How I can solve this problem?

3
  • Where is the JavaScript code located? Wrapping it around .ready would probably be a step forward. Commented Nov 29, 2013 at 16:35
  • There are no elements with the class editPersonalInfo in that code ? Commented Nov 29, 2013 at 16:36
  • @adeneo editPersonInfo is a button's element class. Commented Nov 29, 2013 at 16:37

1 Answer 1

2

You dont have any editPersonalInfo class in HTML markup. Use #eidtPersonalInfoTbl instead.

Try:

$(document).on('click', '#eidtPersonalInfoTbl', function () {
    var obj = modal(); //store object in a variable
    alert(obj['Name']);
    alert(obj['Address']);
    alert(obj['Area']);
});

function modal() {
    var table = $("#eidtPersonalInfoTbl");
    var trs = table.find('tr');
    var obj = {};
    $(trs).each(function (index, row) {
        var field = $(row).find('td').eq(0).text();
        var value = $(row).find('td').eq(1).text();
        obj[field] = [value];
    });
    return obj; //return object
}

Fiddle here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.