kanuaravind
asked on
Default Value of select
I have a validation to check whether the value in the form is changed. I have a problem when the component is a select. I have written a javascript but it is not working in some cases.
My funciton is:
function isChanged() {
for(i=0;i<document.forms[0 ].elements .length;i+ +) {
if(document.forms[0].eleme nts[i].typ e=="select -one) {
for(j=0;j<document.forms[0 ].elements [i].option s.length;j ++) {
if(document.forms[0].eleme nts[i].opt ions[j].se lected !=
document.forms[0].elements [i].option s[j].defau ltSelected ) {
return true;
}
}
}
}
}
In my case i dont have a default value selected in the combo box. I am using the html:select.
My funciton is:
function isChanged() {
for(i=0;i<document.forms[0
if(document.forms[0].eleme
for(j=0;j<document.forms[0
if(document.forms[0].eleme
document.forms[0].elements
return true;
}
}
}
}
}
In my case i dont have a default value selected in the combo box. I am using the html:select.
If you use defaultSelected, you must have
<option selected>
If you don't, it won't work.
You can assume that the default selected is 0, though, since if you don't specify it, the browser will automatically make it option 0. So you can use this code:
function isChanged() {
var selIndex=0;
for(i=0;i<document.forms[0 ].elements .length;i+ +) {
if(document.forms[0].eleme nts[i].typ e=="select -one") {
for(j=0;j<document.forms[0 ].elements [i].option s.length;j ++) {
if(document.forms[0].eleme nts[i].opt ions[j].de faultSelec ted){selIn dex=j;}
}
if(document.forms[0].eleme nts[i].opt ions.selec tedIndex!= selIndex){ return true;}
}
}
return false;
}
<option selected>
If you don't, it won't work.
You can assume that the default selected is 0, though, since if you don't specify it, the browser will automatically make it option 0. So you can use this code:
function isChanged() {
var selIndex=0;
for(i=0;i<document.forms[0
if(document.forms[0].eleme
for(j=0;j<document.forms[0
if(document.forms[0].eleme
}
if(document.forms[0].eleme
}
}
return false;
}
Hi,
If you are using a select, the default value is usually option[0]. So, you should only need to check the selectedIndex:
function isChanged()
{
for(i=0;i<document.forms[0 ].elements .length;i+ +)
{
if(document.forms[0].eleme nts[i].typ e=="select -one)
{
var selObj = document.forms[0].elements [i];
if (! selObj.options[0].selected )
return true;
}
}
}
Vinny
If you are using a select, the default value is usually option[0]. So, you should only need to check the selectedIndex:
function isChanged()
{
for(i=0;i<document.forms[0
{
if(document.forms[0].eleme
{
var selObj = document.forms[0].elements
if (! selObj.options[0].selected
return true;
}
}
}
Vinny
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi brgivens,
I admit I didn't test what I had written until your last post. It works for me (as long as the quote is closed -- 'select-one')
<script type='text/javascript'>
function isChanged()
{
for(i=0;i<document.forms[0 ].elements .length;i+ +)
{
if(document.forms[0].eleme nts[i].typ e=="select -one")
{
alert(selObj.options[0].se lected)
var selObj = document.forms[0].elements [i];
if (! selObj.options[0].selected )
return (true);
}
}
}
//-->
</script>
</head>
<body>
<form onsubmit='isChanged()'>
<select>
<option >select</option>
<option >select</option>
<option >select</option>
</select>
<input type="submit">
</form>
The presumption I am making is that the first option is the default.
Vinny
I admit I didn't test what I had written until your last post. It works for me (as long as the quote is closed -- 'select-one')
<script type='text/javascript'>
function isChanged()
{
for(i=0;i<document.forms[0
{
if(document.forms[0].eleme
{
alert(selObj.options[0].se
var selObj = document.forms[0].elements
if (! selObj.options[0].selected
return (true);
}
}
}
//-->
</script>
</head>
<body>
<form onsubmit='isChanged()'>
<select>
<option >select</option>
<option >select</option>
<option >select</option>
</select>
<input type="submit">
</form>
The presumption I am making is that the first option is the default.
Vinny
Thanks for the points! Why the B?
function isChanged() {
for(i=0;i<document.forms[0
if(document.forms[0].eleme
for(j=0;j<document.forms[0
if(document.forms[0].eleme
document.forms[0].elements
return true;
}
}
}
}
}
gam3r