Search code examples
javascriptjquerydrop-down-menuselect-options

Change select element option with javascript


I search a lot and no code works for this problem.

I have a select element

$(document).ready(function() {
    $('#clientbutton').click(function(e) {
        $("input#citycode").value(example :2);
    }
}

<select id="citycode">
    <option value="1">city 1</option>
    <option value="2">city 2</option>
    <option value="3">city 3</option>
</select>

But I can't change it with javascript on the client side. I try many other solution in Stackoverflow, but none of them worked.


Solution

  • Change this line

    $("#citycode").value(example :2);
    

    to

    $("#citycode").val(2);
    

    Supposed you have an element with id clientbutton.

    Update: #citycode is a select element, not a input element. David Thomas is right.