2

I am trying to add some extra data using jQuerys .data() to a element on a dropdownlist, the dropdown list is populating property and the ID I want is there, but when I try and access the data is showing up and undefined.

Population code.

 $.each(tutorArray, function(index, item){
        var TID= item.T.Id        
        $.each(item.T.TimeList, function(ind, i) {
            $(ddl).append($('<option></option>').val(i.StartTime).html(i.StartTime + ' - ' + i.EndTime).data({
                TID: TID
            }));
        });
    });  

access code

$(ddl).bind('change', function() {
        if($(this).val() != '') {

            var time = $(this).val(); 

            showConfirmRequestTutorCallDialogue(d, time, $(ddlDurationSelector).val(), $(this).data("TID"), $(ddlStudiesSelector).val())
        }
    });

5 Answers 5

5

You should set data for the element this way (see documentation):

element.data("someKey", someValue);

So your population code could look like this:

$.each(tutorArray, function(index, item){
    var TID= item.T.Id        
    $.each(item.T.TimeList, function(ind, i) {
        var element = $('<option></option>');
        element.val(i.StartTime).html(i.StartTime + ' - ' + i.EndTime);
        element.data("TID", TID);
        $(ddl).append(element);
    });
});  
Sign up to request clarification or add additional context in comments.

1 Comment

I have changed the population code over but when I go to get the data back it is still showing up and undefined.
1

http://api.jquery.com/data/#data-html5

I would try using camel case for your data attribute name, since the documentation shows that html 5 is doing some magic behind the scenes when processing the attributes.

Comments

1

A solution with a modified version of already said approach

Thought its months after you asked the question, still I thought of including it so that it may help other users while they lookup for a similar problem. I happen to bounce on a similar problem recently, and I saw this thread and used the answer by Tadeck to set the data values for a dropdown using jquery.

At first the data values were not beeing set properly as you mentioned as well, but then I twigged the approach and did something (see below) which helped me set/retrieve the data values in jquery.

var element = $('<option></option>');  
element.val('id').text('uiName');  
$(element).data("type", 'mytype');

// Here only change is that, instead of doing a element.data(...) I called the element as a jquery dom element using the $ which somehow happen to set the values properly and i can now retrieve the data fields as well!

Hope this might help other users facing similar prolems

Comments

1

No need to split your values, you can simply do.

var element = $(' <option data-type1="" data-type2=""></option>');

Just add as many fields using data-(any property)

set - $(element).data("type1", yourValues);

get - $("#dropdownID :selected").data("type1");

1 Comment

var element = $('<option data-type1="" data-type2=""></option>');
0

So looks from my research that this is not possible the .data() needs to be on a element not on a sub part. So it cant be attached to a <option> element

What I did instead was to include the data within the value field then split it out when the value was accessed.

Set Code

       var element = $('<option></option>');
       element.val(i.StartTime +"--" +TID).html(i.StartTime + ' - ' + i.EndTime);

Access Code

        var str = $(this).val();
        var substr = str.split('--');
        var time = substr[0];
        var TID = substr[1];

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.