I’ve got a string of html <option> elements contained in the variable options. What I’m trying to do is find the <option> within that string with the attribute, value="YE" and add the attribute selected="selected" to it.
I thought the following might work, but it doesn’t, it simply stops the rest of the script from executing properly:
var options = '<?php echo str_replace(array("\n","\r"), '', $geo_options); ?>';
options.find('option[value=YE]').attr('selected','selected');
If options.find('option[value=YE]').attr('selected','selected'); is commented out, the rest of the script works fine.
I’ve also tried to prepend it with var options = option.find(.., which didn’t work.
So, the question is, how can I in jQuery/JavaScript find an <option> element with a particular value in a string of <option> elements, and add the attribute/value of selected="selected" to it?
Any help would be greatly appreciated!
You’re not making
<option>elements, so you can’t add attributes to them. You’ll first need a<select>element and fill that with<option>s. I’m assuming that works.And then:
Is that what you mean?
edit
What’s in the options var exactly? A string like
"<option value=1>bla</option><option value=2>oele</option><option value=3>bar</option>"?