Changing Label color through JS

Javascript coding ..

Moderators: macek, egami, gesf

gbfar
New php-forum User
New php-forum User
Posts: 24
Joined: Tue Aug 16, 2005 10:40 am
Contact:

Changing Label color through JS

Postby gbfar » Fri Sep 15, 2006 6:52 am

Hi,

I have this code in a JS file, which fill data into different fields once user leaves focus from any of the fields (A separate function which calls the XMLHTTPRequest and then passes the responseText to this function). After filling in data into the fields, it also changes a related variable DL for that field and other fields based on what data is returned from the database. In the last, it also needs to change the color of the related label(s) to specify what kind of data was returned. After going through the loop, it displays in the textareas that it has changed the color to the desired color, but it actually doesn't show the color change on the form. To start with all the fields are GRAY, which are hardcoded in the HTML. Sometimes it will write that the initial color value is NOTHING (blank), which should at least be gray. Everything other than the color change on the form works fine. What I am doing wrong? Somebody please help.

Thank you in advance!

GBFAR

Here is the code-


Code: Select all

   
// getObject gets the object on the form either by Element or by ID
// getprefixes returns the prefix for some element on the form
// getcommonvalue returns the starting common values of all the data provided to it
// getfieldname returns the name of the field depending on its number

function getDataBackForFields(fieldname, dataarray){
   // getting different prefixes for different elements
        fieldprefix = getprefixes(groupname, "field");
   divprefix = getprefixes(groupname, "div");
   highlprefix = getprefixes(groupname, "highlight");
   hidprefix = getprefixes(groupname, "hidden");
   lprefix = getprefixes(groupname, "label");
   dlprefix = getprefixes(groupname, "dl");
        // maximum fields to process (number of fields on the form)
   maxfields = 13;

   var browsername = navigator.appName;
   if(browsername == "Microsoft Internet Explorer"){
      var arr = dataarray.split("<BR>")
   }else {
      var arr = dataarray.split("<br>")
   }
   //   textarea object on form to display what's going on
   getObject("concon1").value = "Data Received for field: " + fieldname + ", arr.length=" + arr.length + "\n" + getObject("concon1").value
   if(arr.length>1){
                // Returned from database as the element counter. startval specifies the starting element for a field and endval specifies how many elements this field has
      numbers = arr[0].split("/");
      startval = new Array();
      endval = new Array();
      j=0
      for(i=0; i<maxfields; i++){
         j = i * 2
         startval[i] = parseInt(numbers[j], 10)
         endval[i] = parseInt(numbers[j+1], 10)
      }
      for(maincntr=0; maincntr<maxfields; maincntr++){
         fldname = getfieldname(maincntr)
         st = startval[maincntr]
         en = endval[maincntr]
                   //   textarea object on form to display what's going on
         getObject("concon1").value = "Field: " + fldname + ", ST/EN: " + st + "/" + en + "\n" + getObject("concon1").value
         getObject("concon1").value = "Value/Color/DL: " + getObject(fieldprefix+fldname).value + "/" + getObject(lprefix+fldname).style.color + "/" + getObject(dlprefix+fldname).value + "\n" + getObject("concon1").value
         if(en>1){
            arrname = "";
            abc = st
            for(xcnt=0; xcnt<en; xcnt++){
               var startpos = arr[abc].indexOf(">") + 1
               var endpos = arr[abc].indexOf("</")
               var cc = arr[abc].slice(startpos, endpos)                        // this is done to get rid of font tag from this element to show in the textbox
               if(xcnt==0)
                  arrname = cc
               else
                  arrname += "/" + cc
               abc++
            }
            cc = getcommonvalue(arrname)
            if((getObject(fieldprefix+fldname).value).length==0 || (fldname==fieldname)){                   // if the fldname is the fieldname losing focus or the fldname textbox is empty
               if(cc.length>0){
                  changeDLValue(groupname, fldname, "0")
                  getObject(lprefix+fldname).style.color = "darkorange"
                  getObject(fieldprefix+fldname).value = cc
                  getObject(hidprefix+fldname).value = cc
               } else {
                  changeDLValue(groupname, fldname, "2")
                  getObject(lprefix+fldname).style.color = "gray"
               }
            }
            data = "";
            abc = st
            for(i=0; i<en; i++){
               if(i==0)
                  data = arr[abc];
               else
                  data = data + "<br>" + arr[abc]
               abc++
            }
            if(parseInt(getObject(dlprefix+fldname).value,10)!=1)
               getObject(divprefix+fldname).innerHTML = data
            getObject(divprefix+fldname).style.display=""
         } else if(en==1){
            var startpos = arr[st].indexOf(">") + 1
            var endpos = arr[st].indexOf("</")
            var cc = arr[st].slice(startpos, endpos)
            if((getObject(fieldprefix+fldname).value).length==0 || (fldname==fieldname)){
               getObject(fieldprefix+fldname).value = cc
               changeDLValue(groupname, fldname, "1")
               getObject(hidprefix+fldname).value = cc
               if(fldname==fieldname){
                  getObject(lprefix+fldname).style.color = "green"
               } else {
                  getObject(lprefix+fldname).style.color = "darkorange"
               }
            }
            getObject(divprefix+fldname).innerHTML = arr[st]
            getObject(divprefix+fldname).style.display="none"
         }
         getObject("concon1").value = "End- Value/Color/DL: " + getObject(fieldprefix+fldname).value + "/" + getObject(lprefix+fldname).style.color + "/" + getObject(dlprefix+fldname).value + "\n" +  getObject("concon1").value
      }
   } else if(arr.length==1){
      if(getObject(fieldprefix+fieldname).value){
         getObject(lprefix+fieldname).style.color = "red"
      } else {
         getObject(lprefix+fieldname).style.color = "gray"
      }
      changeDLValue(groupname, fieldname, "2")
      getObject(divprefix+fieldname).style.display="none"
   }
}

Return to “JavaScript”

Who is online

Users browsing this forum: No registered users and 1 guest