0

I have a select box with values automatically assigned and need to convert the values to display a certain string of text further down the page.

For example:

  • Option 1 value 3111 = abc
  • Option 2 value 3112 = def
  • Option 3 value 3113 = ghi
  • etc...

If user selects Option 1 it should reference something saying value equals abc and display abc.

I believe I can grab the value of the dropdown using $(this).val(), but how do I add this in to a string to say if value equals 3111 echo abc?

4 Answers 4

1
$("#options").change(function(){
    switch($(this).val()){
        case "3111":
            alert("abc");
        break;
        case "3112":
            alert("def");
        break;
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0
$("myOption").change(function(){
    $("#displayDiv").html($(this).val());
});

Or use a switch to convert from the value to the display string and insert that instead.

Comments

0

First you have to map dropdown values to your texts

var dropdown_maps = {
    '3111': 'abc',
    '3112': 'def'
}

Then use this to check with your dropdown

$('#my-dropdown').change(function(){

    var v = $(this).val();
    // e.g. v = '3111'
    if (v in dropdown_maps) {
        // result in 'abc';
       console.log(dropdown_maps[v]); 
    }
});

Comments

0

You could just get the value of the drop down directly by examining the selected option.

$('#MySelect').change(function() {
   $('#DisplayDiv').html($('#MySelect option:selected').text());
});​

1 Comment

The 2nd line could be a bit neater: $('#DisplayDiv').html($('#' + $(this).attr('id') + ' option:selected').text());

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.