Craig
Craig

Reputation: 1225

HTML option tag for dropdown selection

I need to be able to store and retrieve an additional ID when creating a dropdown list box. Currently my option values are -

<option value="@x.Id" description="@x.Description" selected="selected">@x.PromotionCode</option>

and then I am able to retrieve the value using -

    $("#discount-selection").change(function () {
        alert ($(this).val())
    });

What other element in the option tag can I use and how do I retrieve it?

Upvotes: 0

Views: 85

Answers (2)

codingpirate
codingpirate

Reputation: 1414

you can use the data-* introduced in HTML5 for storing custom data private to the page or application

Upvotes: 0

Kyle Needham
Kyle Needham

Reputation: 3389

You can you HTML5 data attributes to get anything you like from an element for example.

HTML

<option value="@x.Id" data-description="@x.Description" data-index="0" selected="selected">@x.PromotionCode</option>

JavaScript

$("#discount-selection").change(function ()
{
    var selected = $(this).find(':selected');

    selected.data('description'); // returns @x.Description
    selected.data('index'); // returns 0
});

Live example.

Upvotes: 1

Related Questions