2

I want to call a function on a change of a dropdown and change event will be fired with jquery........................................................................................................................................................................................................................................

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        function a(a) {


            alert('Funtion A');
        }

        $(document).ready(function(){
            $('#sel').change(function(){
                a('a')
            })


            $('button').click(function(){
                $('#sel').click()
                $('#sel').val('B');

            })
        });
    </script>
</head>
<body>
    <select id="sel" >
        <option>A</option>
        <option>B</option>
        <option>C</option>
    </select>

    <button type="button">Change</button>

</body>
</html>
1
  • any error on console? Commented Jan 23, 2019 at 12:25

2 Answers 2

1

If I understand you correctly, you're looking to set the value of a <select> and trigger its change event? That would simply be this:

$('#sel').val('B').trigger('change');

Or, if you prefer to keep it on two lines as in your current attempt:

$('#sel').val('B');
$('#sel').trigger('change');

The former is generally preferred and consumes slightly less resources, but if you personally prefer two explicit statements as you learn then there's no real harm in the latter.

Sign up to request clarification or add additional context in comments.

Comments

0

use the following way to fire events of on other element

$("#sel").trigger("change");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.