views:

241

answers:

2

Been searching for this for over a day on google without any answers. Finally putting fingers to keyboard for this. So frustrating!

I'm trying to learn MooTools and am a TOTAL javascript noobie so please be gentle with me.

Okay, what I'm tying to do is to change the state of a disabled input field (type is text) when a particular option is selected. The html looks a bit like tis:

<select class="wide" id="selectBox" name="option> 
  <optgroup label="Common">
      <option value="one">One<option>
      <option value="two">Two<option>
      <option value="three">Three<option>
  </optgroup>
  <optgroup label="Less Common">
      <option value="other">Other<option>
  </optgroup>
</select>
<input id="other" type="text" disabled="disabled" />

This is what I was HOPING would give me the value to be checked in an if statement that would then change the input disabled to enabled:

window.addEvent('domready', function(){

$$('#selectBox').addEvent('change',function(){ var selection = $$('#selectBox').getSelected(); alert(selection); }); });

So, when the code us run (i.e. I select another value in the option box) all I get is "[object HTMLOptionElement]".

I'm ready to start plucking my hair out one strand at a time... this is so frustrating! The documentation on mootools for this method is SPARSE and only says:

Element Method: getSelected

Returns the selected options of a select element.

Returns:

* (array) An array of the selected elements.

Examples: HTML

<select id="country-select" name="country">
    <option value="US">United States</option
    <option value ="IT">Italy</option>
</select>

JavaScript

$('country-select').getSelected(); //Returns whatever the user selected.

Note:

This method returns an array, regardless of the multiple attribute of the select element. If the select is single, it will return an array with only one item.

Totally confusing!

Someone please tell me what I'm missing... I've also tried:

var selection = $$('#selectBox').getSelected().value; //and
var selection = $$('#selectBox').getSelected('value'); //and
//a WHOLE bunch of other wild ideas including
var selection = $$('#selectBox').getSelected();
alert(selection[0]);

Nothing comes out properly. In some cases I get "undefined" and in other cases I get the same "[object HTMLOptionElement]".

Help!

+1  A: 

So Complex!

You don't need to do such a complex thing, this would suffice:

var selection = document.getElementById("selectBox").value;
alert(selection);

That should get you the selected text.

But if you wanted to use mootools, I guess that this would work (I'm not going to try it)

var selection = $('selectBox').getSelected();
alert(selection[0].value);

Also this has some problems:

<select class="wide" id="selectBox" name="option> 

You don't need the name attribute, as it is basically the same as id. Also if you do have both, then they should probably be the same. I.e. id="selectBox" and name="selectBox

Your name tag should be closed.

Also in your sample, you had a lot of <option>...<option> which should be <option>...</option>

Vincent McNabb
tranquil.meadows
Just coming back to Vincent's comment - tried the second option ( var sel = selection[0].value; alert(sel);)and it worked!!! Thank you very much!
tranquil.meadows
+2  A: 

so many things wrong, not sure where to begin.

$$() is a collection operator (alias for document.getElements() which returns multiples based upon a selector) - not appropriate to use for an id.

you want document.id("idhere") or $("idhere")

for mootools 1.2+

document.id('selectBox').addEvent('change',function() {
    alert(this.get("value")); // get value
    alert(this.getSelected().get("value")); // gets the option that's selected and then it's value
});

make sure you check your markup - you don't close the options, you have a missing " from name="option> as well.

getSelected is there as a method as some selects use multiple selection so doing selectEl.get("value") will not report anything meaningful. any other case, .get("value") is fine.

check it working: http://www.jsfiddle.net/dimitar/SmShF/

have fun and read the manual :)

Dimitar Christoff
Dimitar, sorry, didn't see your response before typing a thanks to Vincent... Thanks to you as well! I understand all you said
tranquil.meadows
Okay, this is a lot of huzza bout nothing but... I tried adjusting the code to use a single $ as opposed to $$:window.addEvent('domready', function(){ $('#selectProvince').addEvent('change',function(){ var selection = $('#selectProvince'); var sel = selection[0].value; if(sel === "otherProvince") { alert(sel); } });});and it doesn't work (with mootools). The $$ does work. Any comments?
tranquil.meadows
yes. `$("selectProvince")` - no # needed. also, within the change callback, `this` is bound to the original selector hence all you need is `var selectedValue = this.get("value"); $("newelement").set("value", selectedValue);` in mootools you tend to use getters and setters to access properties as opposed to .value direct. just try the link i posted on jsfiddle and you will see what i mean.
Dimitar Christoff
Thanks for taking the time to clarify Dimitar. So then, if you want to use CSS style selection then use the $$ otherwise it is just the string-name of the id/selector ('selector'). Got ya. Guess I was all hung up on jQuery style coding. But one thing - what if you have multiple elements that are different types (id, class, name etc.) that have the same string-name (#select, .select, name="select" etc.). What happens then? I know that there's a way to filter the resulting grabbed elements but doesn't just using the $$ instead make more sense (and less code)?
tranquil.meadows
One more time, just wanna thank you for your time and efforts on my behalf, am much obliged. Read over your last explanation a few times before I understood it. I am tied up in assignments in class but am definitely gonna sit after this course is finished and slowly and thoroughly learn javascript (lynda.com et al). :o)
tranquil.meadows
right. basically $$() returns a collection of elements (array of objects) based upon a criteria. for example, a CSS selector to get all divs with class 'foobar' -> `$$("div.foobar")`, get all the first child div of all divs with class foobar -> `$$("div.foobar > div")`. get the first select on the page with name='foo' -> `var select = document.getElement("select[name=foo]");` which would be the same as `$$("select[name=foo]")[0]` but returns false if not found an element. `$("id")` is like `document.getElementById()` on fire - returns an element but also extends it and assigns it a uid.
Dimitar Christoff
you can also narrow things down by a parent nodeid. if you don't want to traverse the all dom elements for a select with name="foo" but know it makes sense to search just child nodes or the form where the select is, do this instead: `var selected = $("formid").getElement("select[name=foo]").get("value");` - i would recommend googling `mootools selectors`
Dimitar Christoff
wow, lots of info. my eyes crossed reading it. still, am very grateful for your response. feel like a fish out of water dealing with coding... thanks for the hints!
tranquil.meadows