question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any way to set the value of a select tag from the tag its self rather than setting selected on the child option tags?

I would like to be able to write:

el("select#kittens", {selected: "two"},
   [el("option", {value: "one"}, "one"),
    el("option", {value: "two"}, "two"),
    el("option", {value: "three"}, "three")])

Rather than:

el("select#kittens",
   [el("option", {value: "one"}, "one"),
    el("option", {value: "two", selected: true}, "two"),
    el("option", {value: "three"}, "three")])

If this is not possible currently is it a feature that you would consider adding to the domvm?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
leeoniyacommented, Oct 10, 2017

Eh, I doubt most people are coming to domvm from vanilla HTML/JS/DOM.

most likely come from jQuery since there are many more of them than the rest combined. this is part of the reason that the lib focuses on distributing a pre-built single file, staying small, tooling-free and as easy to get started with as jQuery.

i expect domvm’s more sophisticated audience to be able to discern JSX-isms (className="..."), React-isms (synthetic camelcase events) and Mithril-isms (withAttr, etc.) from vanilla HTML/JS/DOM. i’m not particularly keen on imitating features from those by adding non-standard special cases unless the value is sufficiently high and does not introduce a perf overhead or add a bunch of extra internal machinery.

to implement this in domvm, i’d need to add handling in at least the hot preProcBody() and either mutate the attrs object of the parent to ensure it doesnt actually set a selected or value attrs on the select or add a bypass check to all the hot attrs loops. if the API was something closer to the more-explicit ivi, eg h.select().attrs().children(), then it would be easier to do without affecting perf lib-wide.

the value-added here isn’t worth it IMO vs having a very simple helper.

1reaction
leeoniyacommented, Oct 6, 2017

i was hoping this would suffice, and it mostly does, except for initial mount 😠: https://jsfiddle.net/740b3ja3/

i agree with @lawrence-dol that it’d be most logical with value. however, it would be a feature that’d be difficult to discover because HTML Doesn’t Work Like That ™️.

given that <option>s cannot contain complex elements or even attributes other than value/selected, i’m tempted not to add anything and just have people use a helper function. it’s not all that scary and doesnt impose a penalty across the whole lib:

function selected(value, optEls) {
  optEls.forEach(el => {
    if (el.attrs.value === value)
      el.attrs.selected = true;
  });
  return optEls;
}

el("select#kittens", selected("two", [
  el("option", {value: "one"}, "one"),
  el("option", {value: "two"}, "two"),
  el("option", {value: "three"}, "three")
]))

autofocus is another one that could use special handling…and to mimic html would only need to work on initial root mount and not any view mount afterwards, though the latter may be more useful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

<select>: The HTML Select element - HTML - MDN Web Docs
Each <option> element should have a value attribute containing the data value to submit to the server when that option is selected.
Read more >
HTML select tag - W3Schools
The <select> element is most often used in a form, to collect user input. The name attribute is needed to reference the form...
Read more >
value attribute on <select> tag not selecting default option
The <select> element does not have a value attribute so that is ignored. So, ...
Read more >
HTML <select> Tag
Use the value attribute to specify the option's value. This is usually a machine-readable value - such as an ID or code of...
Read more >
HTML select Tag | Syntax of the tag, examples, attributes
HTML <select> tag is used to create drop down list of options, which appears when the user clicks on form element, and it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found