0

I have a dropdown, and I want to run a jQuery function when-ever a differnet item is selected. This is my current script:

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script>
$('select[name=dropdown]').change(function() {
    $('#result').html($(this).val());
});
</script>

<div id='result'></div>

<select name="dropdown">
    <option value="1">option 1</option>
    <option value="2">option 2</option>
</select>

How do i get this to work?

2 Answers 2

5

Wrap your script in document.ready handler - DEMO

$(document).ready(function() {
    $('select[name=dropdown]').change(function() {
        $('#result').html($(this).val());
    });
});

Or simply place your script at the very bottom of the page.

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

Comments

0
<script src="http://code.jquery.com/jquery-latest.js"></script>

<script>
$(document).ready( function() {
$('select[name="dropdown"]').change(function() {
    $('#result').html($(this).val());
});
});
</script>

<div id='result'></div>

<select name="dropdown">
    <option value="1">option 1</option>
    <option value="2">option 2</option>
</select>

1 Comment

@PeaceDealer won't learn from this slab of code - I down-voted as an explanation as to why would be helpful.

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.