Lets say I have a selectbox wit 10 predefined amounts and an option "other". If I select "other" i get an input box to give the amount. When I leave that field i wnat the value of the selecbox to be the amount typed in the input box. How would I do that with jquery?
A:
Add this so when you leave the input a new option is created in the select box:
$("input#other-value").blur(function (){
// Create new option
$("<option>")
// Assign value and text
.val($(this).val())
.text($(this).val())
// Append before the "other" option
.insertBefore("select@select-id option[value=other]");
});
Also, please mark answers that satisfies you as "accepted" with the check at the left. If you don't, questions are left open and nobody knows the real answer.
Seb
2009-03-19 15:34:39
well the value doesn't have to be visible in the select box. I thought something like:$("#otheramount").blur(function(){ $("#ppsub_amount").val() = $("#otheramount").val(); });but firebug gives the error: invalid assignment left-hand side
sanders
2009-03-19 15:36:18
You have to do something like this: $("#ppsub_amount").val( $("#otheramount").val() ). Note that val() returns a value, but sets a new one when passed as parameter; you can't assign a value to a function's result.
Seb
2009-03-19 15:39:46
A:
Im assuming that 'selectbox' is equivalent to an HTML 'select' tag. here 'otherInpute' is a text input box and 'someSelect' is a HTML 'select' tag.
var val = $("#otherInput").val();
$("#someSelect option:selected").text(val)
Mike_G
2009-03-19 15:36:50
A:
var val = $("#otherInput").val();
$("#someSelect option:selected").text(val)
sanders
2009-03-19 15:43:29