0

I need to replace a value on the page based on what is selected in a dropdown. More specifically I need to fill in the "label" value. Here's the HTML:

<select class="item-number">
  <option>Please select</option>
  <option value="123" label="Description about 123">123</option>
  <option value="456" label="Description about 456">456</option>
</select>

<span class="item-name">Description shows up here</span>

Now, if someone selects, say, option 123, I would need the text in the span to be replaced with "Description about 123". Is there a way to achieve this? I'm a JS/Jquery newbie and completely out of my depth here. ;)

4 Answers 4

1
$('.item-number').change(function () {
    var lbl = $(this.options[this.selectedIndex]).attr('label');
    $('.item-name').html(lbl);
});

here's the FIDDLE

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

Comments

1

Try this

$(document).on('change','select.item-number', function() {
    $('.item-name').text($(this).find(':selected').attr('label'));
});

Demo

Comments

0

Straight from jQuery documentation

$("select").change(function () {
    var str = "";
    $(".item-number option:selected").each(function (i) {
        if ($(this).attr('label')) 
            str = $(this).attr('label');
        else 
            str = "Description shows up here ";
    });
    $(".item-name").text(str);
}).trigger('change');

Working Fiddle

Make some changes to the above one to make it work more better :)

UPDATE 1(on OP's request):

$('select').on('change',function(){
   label= $(this).find(':selected').val();
   $('.item-name').text(label);
});

Working Fiddle

UPDATE 2:

$("select").change(function () {
    var str = "";
    str = $(".item-number option:selected").val();
    if (str == "Please select") {
        $(".no-add").show();
        $(".add").hide();
    } else {
        $(".no-add").hide();
        $(".add").show();
    }
    $(".item-name").text(str);
}).trigger("change");

Working Fiddle

3 Comments

Thanks very much, this works great too! If I decide NOT to use the label but the standard option value, how would I change the code? Also, I'd like to hide/show a button that allows users to add their selection to a cart. I've added some HTML here jsfiddle.net/EZRXX/1. Would you be so kind as to point me in the right direction? THANK YOU! :)
That is perfect, thanks! Can you just help me get the "add to cart" button right? I've tried this here, but it's not working 100% yet: jsfiddle.net/2MHSz Thanks so much!!!
@KaiBrach Check this Fiddle. You can make modifications to it to make it better :).
0

It's that simple:

$("select.item-number").on("change", function() {
    $(".item-name").text($(this).find(":selected").attr("label"));
});

Fiddle: http://jsfiddle.net/9YLZF/

Cheers!

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.