Results 1 to 3 of 3

Thread: Why getelementsbyclassname not working IE

  1. #1
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Why getelementsbyclassname not working IE

    Hi,

    getelementsbyclassname not working IE, there is any way to solve. Because other browser support, but my user only using IE. I post my code below, pls help.

    javascript code:
    Code:
    function sumVal(){
    		var tot = 0;
    		var check_tot = 0;
    		var n = 0;
    		var m = 0;
    		var txtvalname = '';
    		var txttotal = '';
    		var txtelements = document.getElementsByClassName("txt");
    		var sumElements = document.getElementsByClassName("total");
    		var txtcount = txtelements.length;
    		var totalcount = sumElements.length;
    		var sum = txtcount/totalcount;
    		var subj_value = document.getElementById("subj_value").value;
    		var subj_id = document.getElementById("subj_id").value;
    		var addper = 0;
    		
    		if(subj_value == 3)
    		{
    		var base_mark = new Array(50,50,20);
    		var percent = new Array(30,30,40);
    		}
    		
    		if(subj_value == 6)
    		{
    		var base_mark = new Array(20,20,100,20,100,50);
    		var percent = new Array(10,10,15,10,15,40);
    		}
    		
    		if(subj_value == 8)
    		{
    		var base_mark = new Array(20,20,20,20,20,20,20,20);
    		var percent = new Array(12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5);
    		}
    		
    		if(subj_value == 7)
    		{
    		var base_mark = new Array(20,20,20,20,20,20,20);
    		var percent = new Array(12.5,12.5,12.5,12.5,17,17,16);
    		}
    		
    		if(subj_value == 2 && subj_id == 53)
    		{
    		var base_mark = new Array(60,80);
    		var percent = new Array(40,60);
    		}
    		
    		if(subj_value == 2 && subj_id == 5)
    		{
    		var base_mark = new Array(40,40);
    		var percent = new Array(50,50);
    		}
    		
    		if(subj_value == 2 && subj_id == 14 || subj_id == 15 || subj_id == 16 || subj_id == 17 || subj_id == 18 || subj_id == 19)
    		{
    		var base_mark = new Array(100,100);
    		var percent = new Array(50,50);
    		}
    		
    		for(var i = 0; i < txtelements.length; ++i){
    			txtvalname += txtelements[i].name;
    			txtvalname = txtvalname + "___";
    		}
    		
    		for(var j=0; j< sumElements.length; j++){
    			txttotal += sumElements[j].name;
    			txttotal = txttotal + "___";
    //			alert( txttotal);
    		}
    		//split 
    		txtvalname = txtvalname . split('___');
    		txttotal = txttotal . split('___');
    		
    		for (var i = 0; i<totalcount; i++)
    		{
    			for (var j = 0;j<sum; j++)
    				{
    				if( document.getElementById(txtvalname[n]).value == 'v' || document.getElementById(txtvalname[n]).value == 'V' || document.getElementById(txtvalname[n]).value == '' )
    					{
    					check_tot = parseFloat(parseInt(document.getElementById(txtvalname[n]).value,0)/base_mark[j])*percent[j];
    					if(check_tot == 'NaN' || check_tot == 'NAN'){
    						tot = tot+0;
    					}
    					n++;
    					}
    					else
    					{
    				//		alert(base_mark[j]);
    					tot += parseFloat(parseInt(document.getElementById(txtvalname[n]).value)/base_mark[j])*percent[j];
    					addper = addper + percent[j];
    					n++;
    					if(tot == 'NaN' || tot == 'NAN'){
    						tot = tot+0;
    					}
    
    					}
    				}
    			//	alert(addper);
    				tot = (tot/addper)*100;
    		//display the total
    		document.getElementById(txttotal[m]).value = tot.toFixed(0);
    		m++;
    		tot = 0;
    		addper = 0;
    		}
    		
    	}
    PHP CODE:
    Code:
    print '<tr>';
    					print '<td>' . $this->sname($rs['rp_main_id']) .'</td>';
    					print '<td><table><tr>';
    					foreach($subj_comp as $key => $value)
    					{
    					print '<input type="hidden" name="subj_id" id="subj_id" value="'.$subj_id.'" />';
    					if($rs['subject_id'] == $key)
    					 {
    						for($k=1;$k<=$value;$k++)
    						{
    						print '<td>';
    print '<txt><input class="txt" type="text" name="grd_' . $rs['rp_main_id'] . '_' . $rs['subject_id']. '_' . $k.'" id="grd_' . $rs['rp_main_id'] . '_' . $rs['subject_id']. '_' . $k.'" size="3" maxlength="3"  value="'.$comprs['comp'.$k].'" onkeyup="sumVal();" /></txt></td>';
    						}
    						$tot = $subj_value+1;
    						print '<td><total><input class= "total" type="text" name="grd_' . $rs['rp_main_id'] . '_' . $rs['subject_id']. '_' . $tot .'" ID="grd_' . $rs['rp_main_id'] . '_' . $rs['subject_id']. '_' . $tot .'" value="'.$this->getGrade($rs['rp_main_id'],$rs['subject_id']).'" size="3" maxlength="3" readonly="readonly"  /> <total>  </td>';
    					 }/// check with array "Key" Comp
    					}
    					print '</tr></table></td></tr>';

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It works fine in IE 9+. For IE 8 and less which don't support it, add this code as the first thing in your script:

    Code:
    if (!document.getElementsByClassName) {
    	document.getElementsByClassName = function (cn) { 
    		var rx = new RegExp("(?:^|\\s)" + cn+ "(?:$|\\s)");
    		var allT = document.getElementsByTagName("*"), allCN = [],ac="", i = 0, a;
    			while (a = allT[i=i+1]) {
    			  ac=a.className;
    			  if ( ac && ac.indexOf(cn) !==-1) {
    				if(ac===cn){ allCN[allCN.length] = a; continue;   }
    				rx.test(ac) ? (allCN[allCN.length] = a) : 0;
    			  }
    			}
    		return allCN;
    	}
    }
    From:

    http://www.webdeveloper.com/forum/sh...3&postcount=23
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2009
    Location
    Calgary Alberta
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    @jscheuer1, thank you so much for posting this! I was looking all over for a solution and this one hit it right on the head! Thanks!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •