3

I'm still learning of how to use jQuery but I stumbled with a block and can't get through it. How to append an option to a select with data-* attribute? Currently I'm appending option through this code below:

$('<option>').val(object.val).text(object.object.text).appendTo('#includedItems');

Is there a way to also add data-* to the options?

1
  • Use .attr('data-something','somevalue') to add data-* attribute. That's said, depending your use case, you could instead use the data object .data('something','somevalue') which won't update the DOM attribute Commented Jun 5, 2014 at 9:31

5 Answers 5

7

Use this .attr(),

$('<option>').attr('data-name','abcd');

Or .data()

$('<option>').data('name','abcd');

$('<option>').val(object.val).attr('data-name','abcd').text(object.object.text).appendTo('#includedItems');

Note: .data() stores data in DOM object, NOT as Html attribute while .attr() does.

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

8 Comments

That's not the way to set attribute using jQuery attr() method
.attr('data-name="abcd"'); will only try and read an attribute! You forgot the comma. (Who upvoted this???)
Your second option will likely store the value in the object's data and not as a data- attribute. Again, Who keeps upvoting this??? :)
Hooray... Got there in the end :)
Then you should have only shown the attr() part (as others did) and not complete lines that do nothing (including semi-colons) :) I will delete some of my comments shortly.
|
5

Just add a attr() or data() call to the chain:

use attr() if you want to have an actual data-* attribute:

$('<option>').val(object.val).text(object.object.text).attr('data-x', 'y').appendTo('#includedItems');
                                                       ^ call attr()

Use data() to use jQuery's internal data storage system (doesn't use an attribute)

$('<option>').val(object.val).text(object.object.text).data('x', 'y').appendTo('#includedItems');
                                                       ^ call data()

Comments

2

You can use:

 $('<option data-smdata="value"></option>').val(object.val).text(object.object.text).appendTo('#includedItems');

2 Comments

I would have upvoted you for getting in first, but I don't think adding HTML attributes in strings is generally a good idea (i.e. keep the $('<..>') part to a minimum where possible). Note: This is purely from a maintenance perspective. Cheers.
@TrueBlueAussie: indeed. solution provided by shaunakde is better and feasible than mine. cheers :)
2

You can use:

$('#dropdownId').append($('<option/>', {
     value: value,
     text: displayText,
     selected: isSelected ? true : false,
     'data-description': descriptionText
}));

Comments

1

You can use .attr("data-lala",val) to create an attribute on an element, or .data("lala",val) if you dont mind if that information stays hidden in the DOM (an inspect may not show the right value).

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.