1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

null variable problem

Discussion in 'JavaScript' started by gilgalbiblewheel, Feb 26, 2009.

  1. #1
    I put a long script but the problem is small.
    		var book1 = bobj1.value;
    		var chapter1 = cobj1.value;
    		var verse1 = vobj1.value;		
    		var book2 = bobj2.value;
    		var chapter2 = cobj2.value;
    		var verse2 = vobj2.value;		
    
    		var bcvQuotes = new Array();
    		bcvQuotes[0] = "book1=";
    		bcvQuotes[1] = "&chapter1=";
    		bcvQuotes[2] = "&verse1=";
    		bcvQuotes[3] = "&book2=";
    		bcvQuotes[4] = "&chapter2=";
    		bcvQuotes[5] = "&verse2=";	
    
    		var bcv = new Array();
    		bcv[0] = book1;
    		bcv[1] = chapter1;
    		bcv[2] = verse1;
    		bcv[3] = book2;
    		bcv[4] = chapter2;
    		bcv[5] = verse2;		
    		
    		var urlExtension = document.getElementById('displayurl');
    		var getKeyURL = "";
    		//var getKeyURL = "book1="+book1+"&chapter1="+chapter1+"&verse1="+verse1+"&book2="+book2+"&chapter2="+chapter2+"&verse2="+verse2;
    		urlExtension.innerHTML = "";
    		//urlExtension.innerHTML = "book1="+book1+"&chapter1="+chapter1+"&verse1="+verse1+"&book2="+book2+"&chapter2="+chapter2+"&verse2="+verse2;
    
    		for(a=0; a<bcv.length; a++){
    			if(bcv[a]!= ""){
    				getKeyURL += bcvQuotes[a] + bcv[a];
    				urlExtension += bcvQuotes[a] + bcv[a];
    			}
    		}
    Code (markup):
    I don't know if this is written properly:
    if(bcv[a]!= ""){

    The reason this is happening is because the error shows:
     
    gilgalbiblewheel, Feb 26, 2009 IP
  2. in2clearsky

    in2clearsky Peon

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It might be because cobj1 isn't point to any object. Recheck your text box (i suppose) id/name. Or try to use getElementById(). If this doesn't help, just post a full code.
     
    in2clearsky, Feb 26, 2009 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Yeah you're right. I realized it after posting it. I'm using Ajax and the id wasn't parsed yet.
    Here's the solved version:
    function fillChapters1(){
    	var req = createRequest();
    	if(req){
    		req.onreadystatechange = function(){
    			var c = document.getElementById('showchapterdiv1');
    			if(req.readyState){
    				if(req.readyState == 4){
    					if(req.status == 200){
    						c.innerHTML = req.responseText;
    					}
    				}
    			}
    		}
    			var divIds = new Array();
    		divIds[0] = document.getElementById('showbookdiv1'); //not that it matters
    		divIds[1] = document.getElementById('showchapterdiv1');
    		divIds[2] = document.getElementById('showversediv1');
    		divIds[3] = document.getElementById('showbookdiv2');
    		divIds[4] = document.getElementById('showchapterdiv2');
    		divIds[5] = document.getElementById('showversediv2');
    		
    		var urlExtension = document.getElementById('displayurl');
    		
    //		var getKeyURL = "book1="+book1+"&chapter1="+chapter1+"&verse1="+verse1+"&book2="+book2+"&chapter2="+chapter2+"&verse2="+verse2;
    //		urlExtension.innerHTML = "book1="+book1+"&chapter1="+chapter1+"&verse1="+verse1+"&book2="+book2+"&chapter2="+chapter2+"&verse2="+verse2;
    		
    		urlExtension.innerHTML = "";
    		var getKeyURL = "";
    		
    		var bcv = new Array();
    		var bcvQuotes = new Array();
    		
    		if(divIds[0].innerHTML!= 0){
    			bcvQuotes[0] = "book1=";
    			var bobj1 = document.getElementById('selbook1');
    			bcv[0] = bobj1.value;
    			urlExtension.innerHTML += bcvQuotes[0]+bcv[0];
    			getKeyURL += bcvQuotes[0]+bcv[0];
    			for(a=0; a<6; a++){
    				if(document.getElementById('txtarea_'+[a]).value!= ""){
    					getKeyURL += "&txtarea" + [a] + "=";
    					urlExtension.innerHTML += "&txtarea" + [a] + "=";
    				}
    				getKeyURL += document.getElementById('txtarea_'+[a]).value;
    				urlExtension.innerHTML += document.getElementById('txtarea_'+[a]).value;
    			}
    		}
    		if(divIds[1].innerHTML!= 0){
    			bcvQuotes[1] = "&chapter1=";
    			var cobj1 = document.getElementById('selchapter1');
    			bcv[1] = cobj1.value;
    			urlExtension.innerHTML += bcvQuotes[1]+bcv[1];
    			getKeyURL += bcvQuotes[1]+bcv[1];
    		}
    		if(divIds[2].innerHTML!= 0){
    			bcvQuotes[2] = "&verse1=";
    			var vobj1 = document.getElementById('selverse1');
    			bcv[2] = vobj1.value;
    			urlExtension.innerHTML += bcvQuotes[2]+bcv[2];
    			getKeyURL += bcvQuotes[2]+bcv[2];
    		}
    		if(divIds[3].innerHTML!= 0){
    			bcvQuotes[3] = "&book2=";
    			var bobj2 = document.getElementById('selbook2');
    			bcv[3] = bobj2.value;
    			urlExtension.innerHTML += bcvQuotes[3]+bcv[3];
    			getKeyURL += bcvQuotes[3]+bcv[3];
    		}
    		if(divIds[4].innerHTML!= 0){
    			bcvQuotes[4] = "&chapter2=";
    			var cobj2 = document.getElementById('selchapter2');
    			bcv[4] = cobj2.value;
    			urlExtension.innerHTML += bcvQuotes[4]+bcv[4];
    			getKeyURL += bcvQuotes[4]+bcv[4];
    		}
    		if(divIds[5].innerHTML!= 0){
    			bcvQuotes[5] = "&verse2=";
    			var vobj2 = document.getElementById('selverse2');
    			bcv[5] = vobj2.value;
    			urlExtension.innerHTML += bcvQuotes[5]+bcv[5];
    			getKeyURL += bcvQuotes[5]+bcv[5];
    		}
    	req.open("GET","getChapters1.php?"+getKeyURL,true);
    	req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	req.send(null);
    	}
    }
    Code (markup):
    But I wonder if it would be possible to shrink this code with a for loop? It works as it is but the js page has this times 6, meaning in 6 functions.
     
    gilgalbiblewheel, Feb 26, 2009 IP