JQuery Nested Each Statement
Situation consider for example if we have to get the values form the input tags which is placed inside a table,
eg
<table id="emailListtbl"> <tr id="tr_email0" > <td> <select id="emails" name="emailList" onchange="this.setAttribute('opt', this.value); " > <option value="">select</option> <option value="work">work</option> <option value="home">home</option> <option value="personal">personal</option> </select> </td> <td> <input type="text" id="txtEmail0" value="email" count="0" onblur="emailBlur(this);" onkeydown="emailKeyDown(this);" onfocus="emailFocus(this);" /> </td> <td> <img id="imgEmailRemove0" alt="Remove Email" src="field-delete-icon.png" onclick="removeEmail('tr_email0');" /> </td> <td> <img id="imgEmailAdd0" class="imgAddField" alt="Add Email" src="field-add-icon.png" onclick="generateEmail(this);" /> </td> </tr> </table>
in this we have to get the values of each element (selection box and also in text box) its very simple in jquery.
var eJson = {}; var eOption = ""; var eValue = ""; $('#emailListtbl tr').each(function(d){ $(this).find("td select").each(function(td){ eOption = $(this).attr("opt"); return; }); $(this).find("td input").each(function(inp){ eValue = this.value; return; }); eJson[eOption] = eValue; });
the above code will display all inputs as a array and stored in eJson variable.
Rate this solution
If you think this solution is useful — rate it up! Or dump it otherwise.
page revision: 1, last edited: 27 Apr 2010 12:58