tags:

views:

30

answers:

1

I have a bunch of inputs like this:

<input />
<input />
<select>
    <option>First Option</option>
    <option>Second Option</option>
</select>
<input />

What I need to do is create a string with all the values of the inputs and the text of the selected option.

I actually don't know how many inputs and selects there will be, I just need to grab all their values/text, however many forms there are, and put them into a string. To be extra tricky, I also need to parse the option value. Where it says First Option, I need it to become option1, and Second option to become option2 (this is just manual replacement via regex, there is no pattern, and only a few values to replace).

So for example:

<input value="sample text"/>
<select>
    <option selected="selected">First Option</option>
    <option>Second Option</option>
</select>
<input  value="just an example value"/>

"sample text1stoptionjust an example value"

or:

<input value="an example"/>
<input value="sample text"/>
<select>
    <option selected="selected">First Option</option>
    <option>Second Option</option>
</select>
<select>
    <option>First Option</option>
    <option selected="selected">Second Option</option>
</select>

"an examplesample textoption1option2"

Thanks!

+1  A: 

Change your markup slightly and it becomes easier.

<input value="sample text"/>
<select>
    <option selected="selected" value="option1">First Option</option>
    <option value="option2">Second Option</option>
</select>
<input  value="just an example value"/>

var text = '';
$('input,select').each( function() {
   text += $(this).val();
});

or (using map)

var arr = $.map( $('input,select'), function(i) { return $(i).val(); } );
var text = arr.join('');
tvanfosson
I can't really change the markup that way, but I get the idea. Thanks.
Mark