• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 752
  • Last Modified:

This onload function works great for type "text" - what would the syntax be for type "select"?


Hi,

This onload function works great for type "text" - what would the syntax be for type "select"?

window.onload=function () {
  var text=document.getElementsByTagName('input');
  for (var j=0;j<text.length;j++) {
   var ip=text[j];

   if (ip.type=="text") {
       ip.onchange=function () {
        var td=html_CascadeUpTill(this,'TD');
        var tr=html_CascadeUpTill(this,'TR');
        // Getting the value of the PK
        var pk=tr.lastChild.getElementsByTagName('input')[1].value;
        alert(pk);
        var l_cellindex=td.cellIndex;
        var l_column=g_headers[l_cellindex].id;
        var app=html_GetElement('pFlowId').value;
        var pg=html_GetElement('pFlowStepId').value;
        var get=new htmldb_Get(null,app,'APPLICATION_PROCESS=session_auto_update',pg);
        get.add('G_ITEM1',l_column);
        get.add('G_ITEM2',this.value);
        get.add('G_ITEM3',pk);      
        alert('column='+l_column+',value='+this.value+',pk='+pk);
        gReturn=get.get();
        if (gReturn) { alert(gReturn); this.focus();}
        get=null;
       }
   }
</script>
0
bcarlis
Asked:
bcarlis
  • 5
  • 4
3 Solutions
 
sodalitasCommented:
Only two changes would be necessary. The first is to replace
if (ip.type=="text") {
with
if (ip.type=="select") {

and the second is to replace
this.value
with
this.selectedIndex.value

Assuming that the value associated with the selected option is what you want.  Also, this will only work if there is an option selected.  If that is not the case, then (this.selectedIndex == -1) will be true.  So if you have to handle it, you can do this

if (this.selectedIndex == -1)
   get.add('G_ITEM2',"some default value");
else
   get.add('G_ITEM2',this.value);

and again in your alert
0
 
gops1Commented:
You cannot say this:

if (ip.type=="select")

instead

if (ip.type.indexOf("select")!=-1)


or

if (ip.tagName.toLowerCase()=="select")
0
 
bcarlisAuthor Commented:
solalitas,

INPUT only shows "hidden", "text", "checkbox"
i.e. var text=document.getElementsByTagName('input');

So I tried
  var selectlist=document.getElementsByTagName('select');


window.onload=function () {
  var selectlist=document.getElementsByTagName('select');
  for (var j=0;j<selectlist.length;j++) {
   var select_ip=selectlist[j];
   alert(select_ip.type);
  }
etc...

This shows "select-one" from the alert popup....

What is "select-one" ?




0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
gops1Commented:
Have a look at this example:

<html>
      <head>
            <title>Script Demo Gops</title>
            <script language="JavaScript">
            <head>
                  <script type="text/javascript">
                        function getName(obj) {
                              if(obj.type=="text"){
                                    alert("Text Box");
                              }else if(obj.type=="radio"){
                                    alert("Radio button");
                              }else if(obj.type=="checkbox"){
                                    alert("Check Box");
                              }else if(obj.type.indexOf("select")!=-1){
                                    alert("Select");
                              }
                        }
                  </script>
            </head>
<body>
      <input type="text" onClick="getName(this)"><br>
      <input type="radio" onClick="getName(this)"><br>
      <input type="checkbox" onClick="getName(this)"><br>
      <select onClick="getName(this)">
            <option>A
            <option>B
            <option>C
      </select>
</body>
</html>
0
 
gops1Commented:
By default <select> tag is for single choice and thats the reason why you get "select-one" if it is multiple choice, then it gives "select-multiple":

Here is the revised code. just check this out:

<html>
      <head>
            <title>Script Demo Gops</title>
            <head>
                  <script type="text/javascript">
                        function getName(obj) {
                              if(obj.type=="text"){
                                    alert("Text Box");
                              }else if(obj.type=="radio"){
                                    alert("Radio button");
                              }else if(obj.type=="checkbox"){
                                    alert("Check Box");
                              }else if(obj.type=="select-one"){
                                    alert("Single Select");
                              }else if(obj.type=="select-multiple"){
                                    alert("Multiple Selection");
                              }
                        }
                  </script>
            </head>
<body>
      <input type="text" onClick="getName(this)"><br><br>
      <input type="radio" onClick="getName(this)"><br><br>
      <input type="checkbox" onClick="getName(this)"><br><br>
      <select onClick="getName(this)">
            <option>A
            <option>B
            <option>C
      </select><br><br>

      <select onClick="getName(this)" size="4" multiple>
            <option>A
            <option>B
            <option>C
      </select>
</body>
</html>
0
 
bcarlisAuthor Commented:
This is working to a point... It is not getting the VALUE in
alert('COLUMN='+l_column+',VALUE='+ this.selectedIndex.value+',PK='+pk);

It is showing the COLUMN, VALUE is undefined, and PK is correct.

 var selectlist=document.getElementsByTagName('select');
  for (var k=0;k<selectlist.length;k++) {
     var select_ip=selectlist[k];
     //alert(select_ip.type);
     if (select_ip.type=="select-one") {
         //alert(select_ip.type);
         select_ip.onchange=function () {
            var td=html_CascadeUpTill(this,'TD');
            var tr=html_CascadeUpTill(this,'TR');
            // Getting the value of the PK
            var pk=tr.lastChild.getElementsByTagName('input')[1].value;
            //alert('PK='+pk);
            var l_cellindex=td.cellIndex;
            var l_column=g_headers[l_cellindex].id;
            var app=html_GetElement('pFlowId').value;
            var pg=html_GetElement('pFlowStepId').value;
            var get=new htmldb_Get(null,app,'APPLICATION_PROCESS=session_auto_update',pg);
            get.add('G_ITEM1',l_column);
            get.add('G_ITEM2',this.selectedIndex.value);
            get.add('G_ITEM3',pk);      
            alert('COLUMN='+l_column+',VALUE='+ this.selectedIndex.value+',PK='+pk);
            gReturn=get.get();
            if (gReturn) { alert(gReturn); this.focus();}
            get=null;
         }
     }
  }
0
 
gops1Commented:
It is:

this[this.selectedIndex].value
0
 
bcarlisAuthor Commented:
AWESOME!

Final working portion:

  var selectlist=document.getElementsByTagName('select');
  for (var k=0;k<selectlist.length;k++) {
     var select_ip=selectlist[k];
     //alert(select_ip.type);
     if (select_ip.type=="select-one") {
         //alert(select_ip.type);
         select_ip.onchange=function () {
            var td=html_CascadeUpTill(this,'TD');
            var tr=html_CascadeUpTill(this,'TR');
            // Getting the value of the PK
            var pk=tr.lastChild.getElementsByTagName('input')[1].value;
            //alert('PK='+pk);
            var l_cellindex=td.cellIndex;
            var l_column=g_headers[l_cellindex].id;
            var app=html_GetElement('pFlowId').value;
            var pg=html_GetElement('pFlowStepId').value;
            var get=new htmldb_Get(null,app,'APPLICATION_PROCESS=session_auto_update',pg);
            get.add('G_ITEM1',l_column);
            get.add('G_ITEM2',this[this.selectedIndex].value);
            get.add('G_ITEM3',pk);  
            alert('COLUMN='+l_column+',VALUE='+ this[this.selectedIndex].value+',PK='+pk);    
            gReturn=get.get();
            if (gReturn) { alert(gReturn); this.focus();}
            get=null;
         }
     }
  }

You guys are great!
0
 
bcarlisAuthor Commented:

I should be able to consolidate all three somehow... shouldn't I?

<script type="text/javascript">
  var g_table=html_CascadeUpTill(html_GetElement('SESSION_TITLE'),'TABLE');
  var g_headers=g_table.rows[0].cells;

window.onload=function () {
  var selectlist=document.getElementsByTagName('select');
  for (var k=0;k<selectlist.length;k++) {
     var select_ip=selectlist[k];
     //alert(select_ip.type);
     if (select_ip.type=="select-one") {
         //alert(select_ip.type);
         select_ip.onchange=function () {
            var td=html_CascadeUpTill(this,'TD');
            var tr=html_CascadeUpTill(this,'TR');
            // Getting the value of the PK
            var pk=tr.lastChild.getElementsByTagName('input')[1].value;
            //alert('PK='+pk);
            var l_cellindex=td.cellIndex;
            var l_column=g_headers[l_cellindex].id;
            var app=html_GetElement('pFlowId').value;
            var pg=html_GetElement('pFlowStepId').value;
            var get=new htmldb_Get(null,app,'APPLICATION_PROCESS=session_auto_update',pg);
            get.add('G_ITEM1',l_column);
            get.add('G_ITEM2',this[this.selectedIndex].value);
            get.add('G_ITEM3',pk);  
            alert('COLUMN='+l_column+',VALUE='+ this[this.selectedIndex].value+',PK='+pk);    
            gReturn=get.get();
            if (gReturn) { alert(gReturn); this.focus();}
            get=null;
         }
     }
  }
  var text=document.getElementsByTagName('input');
  for (var j=0;j<text.length;j++) {
    var ip=text[j];
    if (ip.type=="text") {
       ip.onchange=function () {
          var td=html_CascadeUpTill(this,'TD');
          var tr=html_CascadeUpTill(this,'TR');
          // Getting the value of the PK
          var pk=tr.lastChild.getElementsByTagName('input')[1].value;
          //alert(pk);
          var l_cellindex=td.cellIndex;
          var l_column=g_headers[l_cellindex].id;
          var app=html_GetElement('pFlowId').value;
          var pg=html_GetElement('pFlowStepId').value;
          var get=new htmldb_Get(null,app,'APPLICATION_PROCESS=session_auto_update',pg);
          get.add('G_ITEM1',l_column);
          get.add('G_ITEM2',this.value);
          get.add('G_ITEM3',pk);      
          //alert('column='+l_column+',value='+this.value+',pk='+pk);
          gReturn=get.get();
          if (gReturn) { alert(gReturn); this.focus();}
          get=null;
       }
   }
   if (ip.type=="checkbox") {
     ip.onclick=function (){
       if (this.checked && confirm('Are you sure you want to delete this row?')) {
          var td=html_CascadeUpTill(this,'TD');
          var tr=html_CascadeUpTill(this,'TR');
          var pk=tr.lastChild.getElementsByTagName('input')[1].value;
          var app=html_GetElement('pFlowId').value;
          var pg=html_GetElement('pFlowStepId').value ;
          var get=new htmldb_Get(null,app,'APPLICATION_PROCESS=delete_emp_ajax',pg);
          get.add('G_ITEM1',pk);
          alert('Deleting pk='+pk);
          gReturn=get.get();
          if (gReturn) alert(gReturn);
          else {tr.parentNode.removeChild(tr);g_table.normalize();}
          get=null;

       }
       else this.checked=false;
     }
   }
  }
};
function bc_AddTableRow(pThis,pColumnHeading,pNum){
      var myTable=html_CascadeUpTill('SESSION_TITLE','TABLE');            
        for(i=0;i<pNum;i++){
        var ogTR = myTable.rows[1];
        var trClone = ogTR.cloneNode(true);
        var last_child=trClone.lastChild.getElementsByTagName('input')
        for (var j=0;j<last_child.length;j++)
        if (last_child[j].type=="hidden" && last_child[j].name=="f01")
                         last_child[j].value="";
              if(document.all){
                  myNewRow = myTable.insertRow(myTable.rows.length);
                  oReplace = myNewRow.replaceNode(trClone);
              }else{
                  myTable.appendChild(trClone);
              }
  }
}
</script>
0
 
bcarlisAuthor Commented:
Is there a way to get all objects?

to replace the individual
var text=document.getElementsByTagName('input');
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now