Scope problem in my JavaScript?

Discussion in 'JavaScript' started by poemind, Mar 26, 2007.

  1. #1
    ==============================================================
    Hi, I am having a scope problem in my script.

    I am tying to link 14 checkboxes together so that they must be checked in order from
    top to bottom. I have done this on my local machine, but I need to put this functionality
    into SugarCRM and I have ran into a snag regarding scope. I think.

    Below is the text of my included file:jsinclude.js

    // sets up array of JavaScript id's
    var sales_stages = new Array()
    sales_stages[1]		=	"new_c";
    sales_stages[2]		=	"notcontactedyet_c"	;
    sales_stages[3]		=	"contacted_c";
    sales_stages[4]		=	"decision_maker_c";
    sales_stages[5]		=	"qualified_c";
    sales_stages[6]		=	"demoscheduled_c";
    sales_stages[7]		=	"democomp_c";
    sales_stages[8]		=	"propsent_c";
    sales_stages[9]		=	"contsent_c";
    sales_stages[10]	=	"pendingapproval_c";
    sales_stages[11]	=	"contractrecvd_c";
    sales_stages[12]	=	"tobetrained_c";
    sales_stages[13]	=	"closedlost_c";
    sales_stages[14]	=	"mgr_review_c";
    
    // meant to loop through checkboxes above checkbox[start_num]
    function clear_up_checks( me ,ss ){	
    	
    	alert("inside2 clear_up_checks" + ss); // 
    	start_num = Number(ss) ;  	
    	
    	for( i = start_num ; i <= 14 ; i = i + 1){		
    		document.getElementById(sales_stages[i]).checked = false;
    		alert(document.getElementById(sales_stages[i]));// returns [object] // (tcb) DEBUG
    	}	
    }
    
    // takes sales stage number ss and 
    // returns the value of the previous 
    // originally 'me' referred to a passed in 'this' reference?
    function is_prev_checked( me , ss ){	
    	alert("inside4 is_prev_checked ss=" + sales_stages[ss]);
    	//alert(me);
    	
    	chk = document.getElementById(sales_stages[ss]).checked ;
    	alert("2 chk = " + chk);
    	
    	if( ss == "1"){
    		if (! document.getElementById(sales_stages[ss]).checked ){
    			clear_up_checks( me , ss );
    		}
    		return document.getElementById(sales_stages[ss]).checked ;	
    	}
    	else if(!chk){
    		alert("inside !chk2");			
    		clear_up_checks( me , ss );		
    		alert("after calling clear_up_checks");
    		return false;	
    	}
    	else return is_prev_checked( ss - 1 );	
    }	
    PHP:
    Then this is where it is referenced in the calling file:

    
    <script language="javascript">
    	var myDoc = document ;
    	checkbox.onclick = is_prev_checked;
    </script>
    
    <td class="dataField">
    <span sugar='slot4b'>
    	<input type='hidden' name='notcontactedyet_c' value='0'>
    <input type='checkbox' name='notcontactedyet_c' id='notcontactedyet_c'
    title='{NOTCONTACTEDYET_C_HELP}' 
    value='1' onclick='is_prev_checked( myDoc ,"2");'{NOTCONTACTEDYET_C_CHECKED}>
    </span sugar='slot'></td>
    PHP:

    I am fairly certain that this a scope problem but I don't know how to resolve the issue.
    How do I access the document object from thes included functions?

    Thanks in advance for any help that you give me!

    poemind
     
    poemind, Mar 26, 2007 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Hi poemind,

    In your second block of code, you have 2 inputs with the same name (notcontactedyet_c).

    On IE, getElementById() strangely looks for both "name" and "id" attributes, and stops searching once either of them is found. In your case, when you do getElementById("notcontactedyet_c"), it would return the hidden input field, not the checkbox.

    Putting behind this IE's strange behaviour, I can't see any reason why you want to have 2 input fields in the same form with the same name, since one will overwrite the value of the other anyway.
     
    phper, Mar 26, 2007 IP
  3. poemind

    poemind Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    phper,

    I agree that it is strange, and since this is open source code from SugarCRM, it was not of my invention, therefore, I am wary of changing it lest I break something which I don't understand. Is there anyway of working around this?

    Thanks for your help.

    poemind.
     
    poemind, Mar 27, 2007 IP