Forums Window

Can't get SELECTED Value

Subscribe to Can't get SELECTED Value 11 post(s), 3 voice(s)

 
Avatar Ben 8 post(s)

Hello… I’m currently testing the new UI for my website. I had been using the older version of Prototype Window but I needed compatibility woth Prototype 1.6 so I’m working on installing the new UI.

I have everything working fine for my DIALOG, but I can’t seem to access the selected value of my select.

My select is as follows:

<select id='mySelect'>
<option value='1'>One</option>
<option value='2'>Two</option>
...
</select>

When the user selects “OK” from my dialog I’m calling a function to process form data.

In the old version I could use $F(‘mySelect’) to retrieve the selected value.

In the new version I get the value “0” (zero) every time.

Could you please help me to alert the selected value:

Example:

buttons: [{ title: ‘Allow’, callback: function(win) { alert($F(“mySelect”));} ...

Thanks,

Ben

 
Avatar Ben 8 post(s)

I just ran more testing… It appears that it works, but the selected value never updates. In my code example above, $F(‘mySelect’) would always output the first value “1”
I was getting “0” in my first case becasue my first option was: <option value="0">- Select One -</option>

 
Avatar Ben 8 post(s)

The only way I know to fix this is to make an onchange() event in the select that then updates an hidden field with the value.

<select id="mySelect" onchange="change_value(this);">
...
</select> <input type="hidden" id="hidden__id" value="" />

JAVASCRIPT:

function change_value(id)
{
    var temp =  id.options[id.selectedIndex].value;
    $('hidden_id').value = temp;
}

I really hope I’m overlooking something simple. Does anyone have any idea of a better way?

Thanks.

 
Avatar StarPeak 22 post(s)

Do you wrap your select in a form? $F is a convenience alias of Form.Element.getValue.

You could also give the form element an id and get the form values as hash by using $(‘your-form-id-here’).serialize(true). The select has to have a name attribute than of course.

 
Avatar Ben 8 post(s)

I do not have form tags. I can try adding them…. I’ll get back to you in 10 mins.

 
Avatar Ben 8 post(s)

I added the form tags and eneded up with the same result from $F(‘mySelect’)

When I look at the generate source in my webdev toolbar on firefox, I see that in the source my HTML is repeated. I have a hidden div at the top of my source with the contents for the Dialog. When I call my Dialog, I use the following to load it:

.setContent($(“HiddenDivContent”).cloneNode(true).show())

Any other ideas? I appreciate the help.

 
Avatar Ben 8 post(s)

Here’s some ore information…

When I just make one change to the select it doesn’t update the value. If I make two changes then the correct value is shown for $F(‘mySelect’)

Something is stopping it from working on the first change.

 
Avatar StarPeak 22 post(s)

did you try to give the form an id attribute – let’s say ‘my-form’, give the select a name attribute, and do use a

buttons: [{ title: ‘Allow’, callback: function(win) { alert($H($(“my-form”).serialize(true)).toJSON()); } ...

 
Avatar Ben 8 post(s)

tried that…

Same results in a different way. The alert box showed:

{mySelect“}

This is a large select being drawn from the database. It has about 200 options in it and the first value is zero. It also did the same thing when I make more than one selection from the list. If I change the select value more than once it seems to work just fine.

I appreciate you help. I’m open to trying anything else.

I just don’t see why the older version allowed for $F() and the new version doesn’t.

Thanks,

Ben

 
Avatar Ben 8 post(s)

Today I rebuilt the code from scratch. I even tested a real basic SELECT with three values in it. Any select has this bug.

 
Avatar Seb Administrator 133 post(s)

could you share a full example code please?

Forums Window