Advanced Search

Results 1 to 2 of 2

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

  1. #1
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question JQuery, looping through form fields. How do I get text value of select option

    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?

    Code:
    	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); 
     }

  2. #2
    Join Date
    Aug 2004
    Posts
    9,863
    Thanks
    3
    Thanked 960 Times in 949 Posts
    Blog Entries
    15

    Default

    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">.
    DD Admin

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •