I have the following table element which has a select element in its td. The table id (table1 in the example) can keep changing. So, how can I fetch the selected option using table ID in JQuery?
<table class="indexViews" id = "table1">
<tr>
<td align="center", width="10%" valign="middle"></td>
<td align="center", width="70%" valign="middle">
<select id="indexViewsList" class="indexViewsListWnd" onChange="switchIndexView(this)">
<option id="0" value="0" selected="selected">Index Quick View</option>
<option id="1" value="1">Identifier View</option>
<option id="2" value="2">Commodity</option>
</select></td>
<td align="right", width="10%" valign="middle"><input class="searchIcon" type="image" src="../../images/downloadIcon.gif" alt="Submit" onClick="downloadIndexFile()"></input></td>
</tr>
</table>
I have the following table element which has a select element in its td. The table id (table1 in the example) can keep changing. So, how can I fetch the selected option using table ID in JQuery?
<table class="indexViews" id = "table1">
<tr>
<td align="center", width="10%" valign="middle"></td>
<td align="center", width="70%" valign="middle">
<select id="indexViewsList" class="indexViewsListWnd" onChange="switchIndexView(this)">
<option id="0" value="0" selected="selected">Index Quick View</option>
<option id="1" value="1">Identifier View</option>
<option id="2" value="2">Commodity</option>
</select></td>
<td align="right", width="10%" valign="middle"><input class="searchIcon" type="image" src="../../images/downloadIcon.gif" alt="Submit" onClick="downloadIndexFile()"></input></td>
</tr>
</table>
Share
Improve this question
asked Oct 7, 2013 at 19:12
DineshDinesh
734 silver badges10 bronze badges
3
-
2
Do both
id
s keep changing, or just thetable
element's? What makes them change (and honestly, changing anid
doesn't make a lot of sense, really)? – David Thomas Commented Oct 7, 2013 at 19:14 - 3 First, why does the table id keep changing, and second does the table ID HAVE to keep changing? – kevindeleon Commented Oct 7, 2013 at 19:14
- Only the table ID keeps changing. This is because I have multiple windows which have this table. I append this table element to each window while creating them. I assign a unique table ID to each window while appending. – Dinesh Commented Oct 7, 2013 at 19:17
4 Answers
Reset to default 2I don't understand why your id keep changing. But maybe you will want to iterate through your table using jquery selector on class name.
var lstIndexView = $(".indexViews .indexViewsListWnd");
http://api.jquery./class-selector/
try below code
var selectedValue= $(".indexViews .indexViewsListWnd").val();
You can use this selector in jQuery $('table[id^="table"]')
Jquery - "Attribute starts with" selector
This is how you get it using table id.
$('#table1 #indexViewsList').val();
But better you get this select directly:
$('#indexViewsList').val();