Frans Simanjuntak
Frans Simanjuntak

Reputation: 123

How to get value of text box inside of table using jquery?

I have problems with this code: I can't get the value of textbox there:

here's my codes:

$(document).ready(function () {
    $('#btnsave').click(function () {
        //saveData();
        // for looping the tr id:
        $("tr#mytr", "#mytable").each(function () {

            // how to get value of pos_x in every tr??

        });
    })
});

and here is the html

<table style="width:100%" id="mytable">
   <tr id="mytr">
    <td>
     X position:
    </td>
    <td>
     <input id="xPositionField" name="pos_x"   style="width:100%"/>
     <input id="yPositionField" name="pos_y"  style="width:100%"/>
    </td>
   </tr>
   <tr id="mytr">
    <td>
     Y position:
    </td>
    <td>
     <input id="xPositionField" name="pos_x"   style="width:100%"/>
     <input id="yPositionField" name="pos_y"   style="width:100%"/>
    </td>
   </tr>
   <tr id="mytr">
    <td>
     Width:
    </td>
    <td>
     <input id="xPositionField" name="pos_x"   style="width:100%"/>
     <input id="yPositionField" name="pos_y"   style="width:100%"/>
    </td>
   </tr>
   <tr id="mytr">
    <td>
     Height:
    </td>
    <td>
     <input id="xPositionField" name="pos_x"   style="width:100%"/>
     <input id="yPositionField" name="pos_y"   style="width:100%"/>
    </td>
   </tr>
   <tr id="mytr">
    <td>
     Last save date:
    </td>
    <td>
     <input id="xPositionField" name="pos_x"   style="width:100%"/>
     <input id="yPositionField" name="pos_y"   style="width:100%"/>
    </td>
   </tr>
   <tr>
    <td colspan="2">
     <input type="button"  name="btnsave" value="Save" id="btnsave" >
    </td>
   </tr>
  </table>

in this code, i use same id of tr and some id of every textbox...

Upvotes: 1

Views: 5174

Answers (3)

Chris
Chris

Reputation: 6638

$('input[name="pos_x"]').each(function(){
    var pos_x = $(this).val();
    //Do what you want from here...
});

should do what you want. The reason your code doesn't work properly as written is multiple elements have the same ID. IDs should be unique (classes can be given to more than one element).

Upvotes: 1

Frans Simanjuntak
Frans Simanjuntak

Reputation: 123

$(this).find("#xPositionField").val()

Upvotes: 0

Kalle
Kalle

Reputation: 2293

$(this).children("#xPositionField").val();

jquery cheat sheet http://labs.impulsestudios.ca/jquery-cheat-sheet

btw I think you should use a different id for the pos_y-fields (like yPositionField)

Upvotes: 0

Related Questions