I am having a hidden form with a list which I need to select an option from when a user wants to view this form. I was using var assignee = 'persons name';
jQuery('#edit-form' option[value='+option+']:first').attr("selected", "selected");
I tried doing this, but this is not working as the value in the option tag is not specified with the \ as well, so it won't find a match
jQuery('#edit-form option[value='+option.replace(/\'/,'\\\'')+']:first').attr("selected", "selected");
So is doing it this way the following my only option ?
jQuery('#edit-form option').each(function(){
if(jQuery(this).val() == option) {
jQuery(this).attr("selected", "selected");
return false;
}
});
a Test page
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script>
//test vars
var option1 = "option two";
var option2 = "option '";
var option = option2;
$(document).ready(function(){
$('#edit-form [name=select1] option').each(function(){
if($(this).val() == option) {
$(this).attr("selected", "selected");
return false;
}
});
$('#edit-form [name=select2] option[value='+option+']:first').attr("selected", "selected");
option = option.replace(/\'/,'\\\'');
$('#edit-form [name=select3] option[value='+option+']:first').attr("selected", "selected");
});
</script>
</head>
<body>
<form id="edit-form">
<select name=select1>
<option value="option one">option one</option>
<option value="option two">option two</option>
<option value="option two">option two</option>
<option value="option \'">option \'</option>
<option value="option '">option '</option>
</select>
<select name=select2>
<option value="option one">option one</option>
<option value="option two">option two</option>
<option value="option two">option two</option>
<option value="option \'">option \'</option>
<option value="option '">option '</option>
</select>
<select name=select3>
<option value="option one">option one</option>
<option value="option two">option two</option>
<option value="option two">option two</option>
<option value="option \'">option \'</option>
<option value="option '">option '</option>
</select>
</body>
</html>