Hello-World
Hello-World

Reputation: 9545

get table row data td and select

Here is what I want to do.

In the first row that has tds then a = the text in the first cell and b = the selected value of the drop down that the user selected

How do I do this as my code is not working?

$(document).ready(function () {
   var s = $('table td:first').parents('tr');

   var a = s.eq(0).text();
   var b = s.eq(1).find('option:selected').val();

   alert(a + "   " + b);
});

<table>
   <tbody>
      <tr>
         <th>ID</th>
       </tr>
       <tr>
          <td>
            test
           </td>
           <td>
             <select>
               <option value="yes">yes</option>
               <option selected="selected" value="no">no</option>
             </select>
           </td>
        </tr>
    </tbody>
</table>

Upvotes: 1

Views: 5905

Answers (3)

cedric
cedric

Reputation: 302

there is an option :

    <table>
        <tbody>
            <tr>
                <th>ID</th>
            </tr>
            <tr>
                <td>
                    test
                </td>
                <td>
                    <select id="mydropdown">
                        <option value="yes">yes</option>
                        <option selected="selected" value="no">no</option>
                    </select>
                </td>
            </tr>
        </tbody>
    </table>​​​



$(document).ready(function () {

    var s = $('table td:first');

    var a = s.text();
    var b = $("#mydropdown option:selected").text();

    alert(a + "   " + b);

});​

Upvotes: 1

Sumit Neema
Sumit Neema

Reputation: 480

You can Also Use the Following Code

$(document).ready(function () {

var s = $('table td:first');

var a = s.html();
var b = s.parents('tr').children().find('option:selected').val();

alert(a + "   " + b);

});

Upvotes: 1

Tats_innit
Tats_innit

Reputation: 34107

Working demo http://jsfiddle.net/snU97/

Rest feel free to play around, & hope it helps your needs :)

code

$(document).ready(function () {

    var s = $('table td:first').parents('tr');

    var a = s.find('td').eq(0).text();//s.eq(0).text();
    var b = s.find('td').eq(1).find('option:selected').val();

    alert(a + "   " + b);

});​

Upvotes: 2

Related Questions