Solved

Default Value of select

Posted on 2004-04-04
6
1,813 Views
Last Modified: 2008-03-10
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].elements[i].type=="select-one) {
        for(j=0;j<document.forms[0].elements[i].options.length;j++) {
            if(document.forms[0].elements[i].options[j].selected !=
                        document.forms[0].elements[i].options[j].defaultSelected) {
                return true;
          }
        }
    }
 }
}

In my case i dont have a default value selected in the combo box. I am using the html:select.
0
Comment
Question by:kanuaravind
6 Comments
 
LVL 7

Expert Comment

by:gam3r_3xtr3m3
ID: 10751969
maybe...:

function isChanged() {
 for(i=0;i<document.forms[0].elements.length;i++) {
    if(document.forms[0].elements[i].type=="select") {
        for(j=0;j<document.forms[0].elements[i].options.length;j++) {
            if(document.forms[0].elements[i].options[j].selected !=
                      document.forms[0].elements[i].options[j].defaultSelected) {
                return true;
         }
        }
    }
 }
}

gam3r
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10752188
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].elements[i].type=="select-one") {
        for(j=0;j<document.forms[0].elements[i].options.length;j++) {
            if(document.forms[0].elements[i].options[j].defaultSelected){selIndex=j;}
        }
        if(document.forms[0].elements[i].options.selectedIndex!=selIndex){return true;}
    }
 }
return false;
}
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 10752198
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].elements[i].type=="select-one)
     {
         var selObj = document.forms[0].elements[i];
         if (! selObj.options[0].selected)
             return true;
      }
  }
}

Vinny
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 7

Accepted Solution

by:
brgivens earned 150 total points
ID: 10752851
Vinny's right, you only need to check the selectedIndex.  His code won't work though - try this:

function isChanged() {
 var i,o;
 for(i=0;i<document.forms[0].elements.length;i++) if((o=document.forms[0].elements[i]).type=="select-one" && !o.options[o.selectedIndex].defaultSelected) return true;
 return false;
}
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 10753084
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].elements[i].type=="select-one")
     {

alert(selObj.options[0].selected)

         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
0
 
LVL 7

Expert Comment

by:brgivens
ID: 10849284
Thanks for the points!  Why the B?
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now