Click to See Complete Forum and Search --> : SelectedIndex always assigns (0,1,2..) values, but my real values are different!


erick30
06-30-2004, 06:29 AM
Hi!

In my website I have several ‘select’ objects that retrieves its data from the database, and its values are many times different to (0, 1, 2, 3, 4,..), I mean the normal index value that javascript assigns to these items. So, the problem is for example: if the select second value is ‘5’, when I write a javascript function with a variable referring to the selected value, the value that is sent to the server if the user select the second item is ‘2’, and the real value retrieved from the database is ‘5’, so, I want to send '5' value not '2'.

Example 1:

function checkValue() {

var userType = form1.userT.options[userT.SelectedIndex].value

if (userType == "5") { ‘ I mean, the second item value selected

alert("some text…")
form1.userType.focus()
return false; }

...


Example 2:

function addArticle()
{

for(var i=0; i<document.getElementById('selectedCert').length; i++)
{
if(document.getElementById('selectedCert').options[i].value == document.getElementById('QualityCert').options[document.getElementById('QualityCert').selectedIndex].value)
{
alert("Already exists");
return false;
}
}

document.getElementById('selectedCert').options[document.getElementById('selectedCert').options.length]=new Option (document.getElementById('QualityCert').options[document.getElementById('QualityCert').selectedIndex].text, document.getElementById('QualityCert').options[document.getElementById('QualityCert').selectedIndex].value);

}



How can I solve this problem?

Thank you,
Erick

Kor
06-30-2004, 06:55 AM
the value that is sent to the server if the user select the second item is ‘2’,


Nope. the value sent to the server is the select's value, not the selelected index. More than that, the select value is the same with the selected option's value.

<select>
<option value="14">104</option>
<option value="15">105</option>
</select>

Now, if second option is selected, the values are:

object.selectedIndex = 2
object.value = object.options[object.selectedIndex].value = '15'
object.options[object.selectedIndex].text = '105'

The value sent to the server is only one, object.value

Note that first value is a number, the other two are strings.

Kor
06-30-2004, 07:03 AM
soryy
object.selectedIndex = 1
but this does not change the argumantation

erick30
06-30-2004, 07:19 AM
Ok! I see..

Then the best way to access to select values or text are:?

Form_Name.Object.options[Object.SelectedIndex].value
Or
Document.Form_Name.Object.options[Object.SelectedIndex].value
Or
Document.Forms[0].Object.options[Object.SelectedIndex].value
Or
Document.Form[0].Object.SelectedIndex
Or
Document.getElementById('Object').options[document.getElementById('Object').selectedIndex].value

Thank you!

erick30
06-30-2004, 08:20 AM
Which is the best way of my list?:

Form_Name.Object.options[Object.SelectedIndex].value ?
Or
Document.Form_Name.Object.options[Object.SelectedIndex].value ?
Or
Document.Forms[0].Object.options[Object.SelectedIndex].value ?
Or
Document.Form[0].Object.SelectedIndex ?
Or
Document.getElementById('Object').options[document.getElementById('Object').selectedIndex].value ?

Kor
07-01-2004, 02:50 AM
1. Form_Name.Object.options[Object.SelectedIndex].value
this will work only in IE. The complete reference should start with document

2. document.Form_Name.Object.options[document.forms[0].Object.selectedIndex].value
and
document.forms[0].Object.options[document.forms[0].Object.selectedIndex].value
are both correct and, if the form is the first form in page and if the form's name is "Form_Name" , both code lines are in fact the same thing

NOTE keep in mind that javascript is case sensitive! (see the red coloured)

But, as I said, there is no need to point the option's value, as the Select value is, by default the selected option's value

Thus it is enough to have

document.forms[0].elements['select_name'].value

3. document.Form[0].Object.selectedIndex will return something else, the index not the value

4. document.getElementById('select_id').options[document.getElementById('select_id').selectedIndex].value is also correct but, as I said, it is enough to use

document.getElementById('select_id').value

NOTE see that name and id are not the same thing. In case 4. you must be sure that your element has an id also.

As far as I see you are not familiar with the form reference. Let's see them

The following 3 variant are all correct:

1. document.forms['name_or_order'].elements['name_or_order'].attribute

2. document.forms['name_or_order'].element's_name.attribute

3. document.form's_name.element's_name.attribute

The first is, I think, the better, as you can use loop statement for dinamically manipulate the elements.

erick30
07-01-2004, 07:08 AM
Thank you very much for your explanation Kor! :)