开发者

Remove/add text based on user selection from drop drown w/ Jquery

开发者 https://www.devze.com 2022-12-27 07:40 出处:网络
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 attac

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?


<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(); 
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号