splanton
asked on
JQuery dynamic change of descriptions in dropdownlist
I have a dynamically vreated dropdownlist with some monetary ranges show like this:
I have the following code:
I would like it to show the values like this:
Or this:
I would like to use the JQuery to set a value in the #currencyindicator span on change of currency (another dropdownlist on the same form).
As a quick and dirty test set I set a hard coded value to see if I could get the dropdownlist description updated. It didn't work!
Doesn't seem to be cutting it... ideas?
Regards,
0 to 500
501 to 1000
1001 to 2000
...
I have the following code:
<option value="<%=(RS_Fee01("FeeId"))%>"><span id="currencyindicator"></span><%=(RS_Fee01("AmountFrom"))%> to <span id="currencyindicator"></span><%=(RS_Fee01("AmountTo"))%></option>
I would like it to show the values like this:
£0 to £500
£501 to £1000
£1001 to £2000
...
Or this:
$0 to $500
$501 to $1000
$1001 to $2000
...
I would like to use the JQuery to set a value in the #currencyindicator span on change of currency (another dropdownlist on the same form).
As a quick and dirty test set I set a hard coded value to see if I could get the dropdownlist description updated. It didn't work!
$('#currencyid').change(function() {
$('span#currencyindicator').text("£");
});
Doesn't seem to be cutting it... ideas?
Regards,
leakim971 is much better at this stuff then I am and I am sure this can be improved.
I noticed you are using asp to dynamically generate your code. One option would be to use asp to write out your js. If you need to write out html. Something like below where you are writing your range in the html option data-from and data-to. You can then access that via jquery to generate the option text.
http://jsbin.com/EyoyEFU/1/edit
I noticed you are using asp to dynamically generate your code. One option would be to use asp to write out your js. If you need to write out html. Something like below where you are writing your range in the html option data-from and data-to. You can then access that via jquery to generate the option text.
http://jsbin.com/EyoyEFU/1/edit
<select id="currency">
<option value="$">$</option>
<option value="£">£</option>
</select>
<select id="money">
<option value="1" data-from="0" data-to="500"></option>
<option value="2" data-from="501" data-to="1000"></option>
<option value="3" data-from="1001" data-to="2000"></option>
</select>
$(function() {
var currencyindicator='$';
$('select#currency').change(function(){
currencyindicator=$(this).val();
$('#money option').each(function(){
var from=$(this).attr('data-from');
var to=$(this).attr('data-to');
$(this).text(currencyindicator+from+'-'+currencyindicator+to);
});
});
$('#money option').each(function(){
var from=$(this).attr('data-from');
var to=$(this).attr('data-to');
$(this).text(currencyindicator+from+'-'+currencyindicator+to);
});
});
<select id="money">
<option value="1" data-from="0" data-to="500"></option>
<option value="2" data-from="501" data-to="1000"></option>
<option value="3" data=from="1001" data-to="2000"></option>
</select>
convert to <option value="<%=(RS_Fee01("FeeId"))%>" data=from="%=(RS_Fee01("AmountFrom"))%>" data-to="%=(RS_Fee01("AmountTo"))%>"></option>
Also, remember in your html, id's are unique, classes are not.
In your original code you had, <span id="currencyindicator">. Besides the fact the span does not work in the option, it would have created multiple id's. Had that worked, you would have wanted to use a class instead of id.
In your original code you had, <span id="currencyindicator">. Besides the fact the span does not work in the option, it would have created multiple id's. Had that worked, you would have wanted to use a class instead of id.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops - Forgot the fiddle - http://jsfiddle.net/ChrisS tanyon/AbS ML/
>Always good to have options:
and a very good one at that!
and a very good one at that!
Why not just use CSS :before or am I missing something?
e.g.
li span:before{
content: "$";
}
e.g.
li span:before{
content: "$";
}
@Gary - the OP needs to dynamically change <options> based on another dropdown.
Ahh thought it was a UL not a select - didn't read that properly.
Open in new window