View Full Version : :: changing values of select elements ::
babelfish
11-13-2002, 05:33 PM
http://www.simonsgroup.com/test/packages/index.html
ok i have the above page...
now, i wanted a sceipt to check the values of the 3 selects and see if they are *, if all 3 are * (used as wildcard) then the form mustnt submit
any ideas why this dont work:
function checkEm() {
if ((document.listings.package.value='*') && (document.listings.zone.value='*') && (document.listings.doctype.value='*')) {
alert('Please select at least one option from the menus');
}
}
thanks all!
beetle
11-13-2002, 05:51 PM
Well, it could be because you are using the assignment operator (=) instead of the comparison/equality operator (==)
(document.listings.package.value='*')
:p
Oh, and make it easy on yourself and declare a reference variable for the form.function checkEm() {
var f = document.listings;
if (f.package.value=='*' && f.zone.value=='*' && f.doctype.value=='*') {
alert('Please select at least one option from the menus');
}
}
glenngv
11-14-2002, 02:43 AM
the correct way to get the value of a select is:
objSelect.options[objSelect.selectedIndex].value
where objSelect is the reference to the select tag
so the function should be like this:
function checkEm() {
var f = document.listings;
if (f.package.options[f.package.selectedIndex].value=='*' &&
f.zone.options[f.zone.selectedIndex].value=='*' &&
f.doctype.options[f.doctype.selectedIndex].value=='*') {
alert('Please select at least one option from the menus');
}
}
babelfish
11-14-2002, 08:55 AM
neither of those scripts work :(
glenngv
11-14-2002, 09:03 AM
why do you call the function on onmousedown?
<input name="Submit" type="submit" value="submit" onMouseDown="checkEm()">
you should do it like this:
<form method="get" name="listings" id="listings" onsubmit="return checkEm(this)">
...
<input name="Submit" type="submit" value="submit">
and modify the function:
function checkEm(f) {
if (f.package.options[f.package.selectedIndex].value=='*' &&
f.zone.options[f.zone.selectedIndex].value=='*' &&
f.doctype.options[f.doctype.selectedIndex].value=='*') {
alert('Please select at least one option from the menus');
return false;
}
return true;
}
babelfish
11-14-2002, 09:15 AM
the idea is that the form works in a very simple way...
takes the values from the 3 select boxes and on submit passes then in the URL to a refreshed page - which i can then read the values and build a search from, so this little script is basically a vallidator to stop ppl from using all wildcards (eventually there will be like hundreds/thousands of diff options to search)
the onMouseDown basically checks to see if the statement is true - if it is it errors and if it isnt it lets you submit as normal
babelfish
11-14-2002, 09:21 AM
still dont work. i dont want it it to submit if all select values are *
Roelf
11-14-2002, 09:26 AM
then you should give your options a value-attribute
babelfish
11-14-2002, 09:29 AM
huh roelf?
can u explain plz?
beetle
11-14-2002, 09:30 AM
Originally posted by Roelf
then you should give your options a value-attribute Hehe, I think that deserves a
http://www.rasta-man.co.uk/images/doh.gif
glenngv
11-14-2002, 09:31 AM
ahh yes, I didnt notice that.
or you can do .text instead of .value, like:
if (f.package.options[f.package.selectedIndex].text=='*' ...
Roelf
11-14-2002, 09:32 AM
i dont know for sure but i always code my options like:
<option value="thevalueyouwanttodosomethingwith">displayedvalue</option>
<edit>you guys beat me with your quick response</edit>
beetle
11-14-2002, 09:32 AM
I can explain. I'll use your first SELECT as an example <select name="package" id="packageID">
<option value="*">*</option>
<option>dog</option>
<option>cat</option>
<option>rat</option>
<option>bat</option>
</select>Of course, you'll probably want to give all your options a value ;)
babelfish
11-14-2002, 09:38 AM
Originally posted by beetle
Hehe, I think that deserves a
http://www.rasta-man.co.uk/images/doh.gif
oh ffs - aint it always the way - the stupid little things u forget to check! lmao!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.