Jquery Click to change the value of a field field

I have this code:

<input type="text" name="frameSelection" id="frameSelection" value="Option 1" />
<a id="change">Change to Option 2</a>

When I click on the link, I want to change the value of the field to "Option 2". How can i do this?

+5
source share
2 answers

Very simple. Use clickto bind an event handler to an event clickand valto set a value:

$("#change").click(function() {
    $("#frameSelection").val("Option 2");
});

As mentioned in other answers, you can disable the default link behavior, but with your code, because at the moment it is in the question (without an attribute hrefin the element a) that is not needed. Keep this in mind if this can change.

+9
source

Using jQuery:

$('#change').click(
    function(e){
        e.preventDefault;  // if your a has an href attribute, this prevents the browser
                           // following that link.
        $('#frameSelection').val("Option 2");
    });

:

+4

All Articles