I just need to add an asterisk to the end of some text based on a user's selection from a dropdown field. If the user select's option one from the drop down then the asterisk is attached and if the user selects the second option than the asterisk is removed. I think I need to do this with jquery but I am having trouble figuring out which function I should use. I tried to wrap the asterisk in a div and toggle the div's css display property with Jquery but this seems really clunky. Is there a better way to go about adding/removing text dynamically?
+2
A:
<select>
<option value="1">First option</option>
<option value="2">Second option</option>
</select>
Some text <span id="asterisk" style="display:none">*</span>
$(document).ready(function() {
// bind to the select's change event
$("select").change(function() {
if($(this).val() == "2") {
$("#asterisk").hide();
} else {
$("#asterisk").show();
}
// fire the event once when the page loads
// so the visible state of the asterisk is based on the selected option
}).change();
});
karim79
2010-04-20 16:17:26
@OP: In other words: The way you're doing it (showing/hiding an element) is perfectly reasonable (which is what my answer said, but karim's got here first, so I deleted it). :-)
T.J. Crowder
2010-04-20 16:19:16
Great, that worked well - thanks
Thomas
2010-04-20 16:33:40