PDA

View Full Version : JQuery, looping through form fields. How do I get text value of select option



Yaresh
08-08-2011, 04:31 PM
1) Script Title: Loop through form fields and get text value of select option

2) Script URL (on DD): http://www.dynamicdrive.com/dynamici...formwizard.htm

3) Describe problem: I am using the code below which works fine. But I want to get the text value of the select option not the value.
The line "HELLO THERE THE TEXT IS"+ ip[i].text() " needs something else, not ".text()"
Any ideas please?


var ip = $('form#genform').find('input, select');
t = "";
for (var i = 0; i < ip.length; i++) {
if ($(ip[i]).attr("name") =='enginetype') {
t+= "<p><span>" + $(ip[i]).prev("label").text() + ":</span> HELLO THERE THE TEXT IS"+ ip[i].text() +'</p>' ;
//$("#list option:selected").text(); ($(this).val()
}
else {
t+= "<p><span>" + $(ip[i]).prev("label").text() + ":</span> " + ip[i].value+'</p>' ;
}

}
alert("t "+t);
$("#datareview").html(t);
}

ddadmin
08-09-2011, 06:29 AM
Well just speaking in generality, $option.text() should get the textual content of an OPTION (the text surrounded by the <option> tag), whereas $option.val() would get the value in <option value="something">.