Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blaze not setting "selected" attribute #3191

Closed
lourd opened this issue Nov 26, 2014 · 18 comments
Closed

Blaze not setting "selected" attribute #3191

lourd opened this issue Nov 26, 2014 · 18 comments

Comments

@lourd
Copy link

lourd commented Nov 26, 2014

_3 Upvotes_ See this repo and this demo.

Basically passing in an empty string to a selected attribute isn't working correctly. When setting multiple attributes as an object, e.g.

// Template
<select>
  <option {{ optionAttrs 'default' }}>Select an option</option>
  <option {{ optionAttrs 'one' }}>Option one</option>
  <option {{ optionAttrs 'two' }}>Option two</option>
  <option {{ optionAttrs 'three' }}>Option three</option>
  <option {{ optionAttrs 'four' }}>Option four</option>
</select>

// Helper
optionAttrs : function ( option ) {
  var attrs = {};
  if ( option === 'default' ) {
    attrs.value = "";
    attrs.disabled = "";
  } else {
    attrs.value = option;
  }
  // If it's option three, make it selected
  attrs.selected = option === 'three' ? "" : false;
  // passing in true instead of empty string didn't change the behavior
  // attrs.selected = option === 'three';
  // Neither did returning 'selected'
  // attrs.selected = option === 'three' ? "selected" : false;
  return attrs;
}

The selected attribute doesn't turn up at all.

When setting the selected attribute explicitly, the correct option is selected (verified by getting the value on the select element), but the option that's supposed to be selected still doesn't actually have a selected attribute.

// Template
<select>
  <option selected={{ isSelected 'default' }}>Select an option</option>
  <option selected={{ isSelected 'one' }}>Option one</option>
  <option selected={{ isSelected 'two' }}>Option two</option>
  <option selected={{ isSelected 'three' }}>Option three</option>
  <option selected={{ isSelected 'four' }}>Option four</option>
</select>

// Template helper
isSelected : function ( option ) {
  return option === 'three' ? "" : false;
}

This does not follow the behavior as documented in the Spacebars README, the Meteor manual, or the wiki article on using Blaze.

I've tested this on OS X 10.10 and 10.9 using Safari, Firefox, and Chrome, and iOS 7 and 8.

@avital avital added the Blaze label Dec 1, 2014
@benjamin79
Copy link

I can confirm this. Same system. Selection works, but doesn´t show in html.

@dgreensp
Copy link
Contributor

dgreensp commented Feb 4, 2015

There does seem to be something funny going on here. Thanks for the repo.

First, to clarify: From poking at the repo, I think it doesn't actually matter if the truthy value you use is "", or true, or "selected" -- the bug is there either way. For the second part (the plain reactive attribute), that behavior is the same as for checkboxes and not necessarily a bug. You won't necessarily see "checked" in the HTML, but it is set in the DOM (the same as if you set it using jQuery).

From a cursory look, it looks like we are setting .selected on the option element too soon -- before it has a parent node -- and it is not taking effect. We will fix.

@geremora
Copy link

It's true, I confirm this. Same issue in checkboxes and option element.

@aadamsx
Copy link

aadamsx commented May 13, 2015

I'm running into this now with the latest release of Meteor. I have to select the option twice for the 'selected' to take.

Any word if this is getting corrected in the next update?

@dkmooers
Copy link

+1, I'm also having this issue. It's preventing me from canceling a select change reactively, I think, unless I'm missing something. E.g. on select change, fire confirm dialog, if cancel, change select back to what it was - and my "isSelected" template helper is re-running properly, but the select doesn't go back to the previous value, I think because Blaze isn't setting the "selected" attribute in the HTML.

@jbrozena22
Copy link

+1 Same issue here. I can't set the default selected item in a dropdown list because of this.

@dkmooers
Copy link

@jbrozena22 Have you tried this:

option(selected="{{isSelected someValue}}" value=someValue) someValue

That's Jade BTW, sorry about that. But what you basically want is:

selected="true"

... I think. I think I remember trying selected="selected" and it didn't work with Meteor. Try returning a boolean from your isSelected helper.

@Cerealkillerway
Copy link
Contributor

+ 1

@jkras
Copy link

jkras commented Jul 8, 2015

+1

@charleshan
Copy link

Any updates on this?

@IstoraMandiri
Copy link

This format works for me

<select name="platform" class='form-control'>
  <option value="osx" selected="{{platformIs 'osx'}}">Mac OSX</option>
  <option value="ubuntu" selected="{{platformIs 'ubuntu'}}">Ubuntu 14</option>
</select>

Where platformIs returns true if this.platform equals the argument.

@AnthonyAstige
Copy link

I've just run into a similar issue and the workaround from @hitchcott seems to have fixed it.

@spencern
Copy link

Also running into this issue and @hitchcott's solution is working as well.

@iwoj
Copy link

iwoj commented Sep 22, 2016

Solution from @hitchcott doesn't work for me. Blaze doesn't seem to render any selected attribute I set, even if hardcoded. Very strange.

@hwillson
Copy link
Contributor

Moved to meteor/blaze#210 - closing here - thanks!

@aisamu
Copy link

aisamu commented Dec 21, 2016

@iwoj Same here...
The funny thing is that data-selected, for example, works perfectly (without @hitchcott's workaround)

@abernix
Copy link
Contributor

abernix commented Dec 27, 2016

In order to avoid conversation getting lost, please continue additional discussion on the new issue within the Blaze repo.

@meteor meteor locked and limited conversation to collaborators Dec 27, 2016
@mitar
Copy link
Contributor

mitar commented Dec 31, 2016

This has been fixed in master of Blaze just now. In fact the second issue (only property being set, not attribute) is by design. The first issue was in fact that false from dynamic attributes (dicts) were seen as non-nully value (in contrast with values from helpers where false was converted to null already).

See this comment for more information.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests