idophir
idophir

Reputation: 14759

Setting a value-only attribute and not a key-value

I want to add a single word attribute to an object.

Should be: <option selected>aaa</option>

Not: <option selected="selected">aaa</option>

Using jQuery or javascript.

Upvotes: 0

Views: 70

Answers (3)

Pehmolelu
Pehmolelu

Reputation: 3564

Use Property.

This changes the currently selected

$(selector).prop('selected',true);

http://api.jquery.com/prop/

EDIT:

http://jsfiddle.net/DH9ac/1/

This changes the defaultSelected

$(selector).prop('defaultSelected',true);

This will now set the default select value, but with the extended format: selected="selected".

Upvotes: 0

Quentin
Quentin

Reputation: 943220

<option selected> is just HTML shorthand for <option selected="selected">. When you view a webpage, your browser will convert the HTML into a DOM (which doesn't care which syntax was used to create it). You can then manipulate the DOM (this is important, you are not manipulating the original HTML, just what the browser has parsed it into).

If you ever serialise the DOM back to HTML then you might get <option selected> or <option selected="selected"> depending on how the serializer was implemented. You will probably do this by asking the browser to provide the innerHTML of an element, in which case which you get is determined by the browser authors and you have no control over it.

If it is important to express the selected attribute using the shorthand form, then you will have to use an alternative seraliser that gives you that control. I'm not aware of any, so you might have to write something from scratch.

Upvotes: 2

Joseph Marikle
Joseph Marikle

Reputation: 78520

document.body.setAttribute("blah","")

That should do it.

Upvotes: 0

Related Questions