How do I set an option value in select list with jQuery? I tried:
$('#SelectGroup :all').replace("tmp4", "abc");
I mean: Search for tmp4 string and replace it with abc. In the list, there are 4 items I don’t mind going directly to entrance #4.
I will extend my question: I want to pass to the function that replaces a value from outside. I tried:
$('#SelectGroup option').each(function(fieldNum, newVal) {
this.text = this.text.replace('tmp4',$(newVal).text());
});
});
and I tried:
$('#SelectGroup option').each(newVal, function() {
this.text = this.text.replace('tmp4',newVal);
});
});
but here it says it fails! in the first line of .each
Why?
I don’t understand! I pass a value to a function but it loses its value in the function and change
var newVal= $(this).val();
$('#SelectGroup option').each(function(fieldNum, newVal) {
alert("final option text is:" + $(this).text());*/
alert($(newVal).text());
this.text = this.text.replace('tmp4',$(newVal).text());
});
});
but newVal is not what I pass to the function – it is what the for each gives it.
Doesen’t $(‘option value=[“‘ + value + ‘”]’) work?