0

I have a form with a simple drop-down menu when the user selects any of the options, i want to be able to pass that selected value to a JavaScript function

Once the user clicks on an option it will alert the value in the jQuery change function but I want to use the value that was passed through in other functions but it does not get the value for some reason

function getTweet(){
  $('#test').change(function() {
    var value = $('#test :selected').val();
  });
}

want to use the value outside of the change function but it does not grab it

<form>  
    <select id="test">
        <option value="one">1</option>
        <option value="two">2</option>
        <option value="three">3</option>
    </select>
</form>
6
  • 1
    Probably a paste issue that your getTweet function is missing the closing }? Commented Apr 19, 2013 at 14:26
  • yea sorry about that paste issue let me edit Commented Apr 19, 2013 at 14:27
  • Please elaborate on "pass a variable after a user selects" as we my be able to better help you. Commented Apr 19, 2013 at 14:30
  • It works fine for me - jsfiddle.net/2xEqT Commented Apr 19, 2013 at 14:42
  • @Ian of course it works well inside the change handler :) Commented Apr 19, 2013 at 14:46

2 Answers 2

4

If you want to use the most current value of the drop-down anywhere in your code, you can just reference it like this:

$('#test').val()

I've removed the :selected, because that's not necessary; .val() will give the right value.

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

Comments

-1

You might use custom events to do something based on that event?

Example:

$('#test').change(function () {
    $('mysubscriberselectorshere').trigger('gettweet');
});

// do something based on the custom event
$('mysubscriberselectorshere').on('gettweet', function {
    // do good stuff here
    alert($('#test').val());
});

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.