www.webdeveloper.com
Results 1 to 4 of 4

Thread: Using a Javascript variable in another Javascript.

  1. #1
    Join Date
    Dec 2004
    Location
    canada
    Posts
    53

    Using a Javascript variable in another Javascript.

    Is it possible for a Javascript variable to be called into a different Javascript?

    If so, what am I doing wrong...

    Code:
    <SCRIPT LANGUAGE="javascript">
    
    function formCheck(formobj){
    	if ((internal.'PHONE' + iteration.value.length < 15)){
    		alertMsg += " - PHONE #1 Requires 12 digits" + "\n";
    		alertMsg += "    You have only entered " + internal.'PHONE' + iteration.value.length + " digits(s).\n";
    	}
    	else{
    		alert(alertMsg);
    		return false;
    	}
    }
    </SCRIPT>	
    
    
    
    <SCRIPT LANGUAGE="javascript">
    function addRowToTable()
    {
    
    var tbl = document.getElementById('tblPHONEadd');
    var lastRow = tbl.rows.length;
    // if there's no header row in the table, then iteration = lastRow + 1
    var iteration = lastRow;
    var row = tbl.insertRow(lastRow);
    
    // OuterOutsideLeft cell
    var cellOuterOutsideLeft = row.insertCell(0);
    var textNode = document.createTextNode('');
    cellOuterOutsideLeft.appendChild(textNode);
    
    // OutsideLeft cell
    var cellOutsideLeft = row.insertCell(1);
    var textNode = document.createTextNode('PHONE ' + iteration + '  ');
    cellOutsideLeft.setAttribute('align','right');
    cellOutsideLeft.appendChild(textNode);
    
    // spacer1 cell
    var cellspacer1 = row.insertCell(2);
    var textNode = document.createTextNode('');
    cellspacer1.appendChild(textNode);
    
    // MiddleLeft cell
    var cellMiddleLeft = row.insertCell(3);
    var el = document.createElement('input');
    el.setAttribute('type', 'text');
    el.setAttribute('name', 'PHONE' + iteration);
    el.setAttribute('size', '20');
    el.setAttribute('maxLength', '12');
    el.onkeypress=function(){if ((event.keyCode < 48) || (event.keyCode > 57)) event.returnValue = false;};
    el.onkeyup=function(){return mask(this.value,this,'3,7','-')};
    el.onblur=function(){return mask(this.value,this,'3,7','-')};
    cellMiddleLeft.appendChild(el);
    el.focus();
    
    </SCRIPT>
    What I need done is to have the formCheck validate the added PHONE #'s with the iteration.
    Is this possible?
    Dave

    ---------------------------------------
    It's the end of the world as we know it, and I feel fine...

  2. #2
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    ARg. that wouldn't even validate, change as so it does:

    PHP Code:
    <SCRIPT type="text/javascript">
    <!--
    //begin

    function formCheck(formobj){
        if ((
    internal.'PHONE' iteration.value.length 15)){
            
    alertMsg += " - PHONE #1 Requires 12 digits" "\n";
            
    alertMsg += "    You have only entered " internal.'PHONE' iteration.value.length " digits(s).\n";
        }
        else{
            
    alert(alertMsg);
            return 
    false;
        }
    }

    //-->
    </SCRIPT>    



    <SCRIPT type="text/javascript">
    <!--//begin
    function addRowToTable()
    {

    var tbl = document.getElementById('tblPHONEadd');
    var lastRow = tbl.rows.length;
    // if there's no header row in the table, then iteration = lastRow + 1
    var iteration = lastRow;
    var row = tbl.insertRow(lastRow);

    // OuterOutsideLeft cell
    var cellOuterOutsideLeft = row.insertCell(0);
    var textNode = document.createTextNode('');
    cellOuterOutsideLeft.appendChild(textNode);

    // OutsideLeft cell
    var cellOutsideLeft = row.insertCell(1);
    var textNode = document.createTextNode('PHONE ' + iteration + '  ');
    cellOutsideLeft.setAttribute('align','right');
    cellOutsideLeft.appendChild(textNode);

    // spacer1 cell
    var cellspacer1 = row.insertCell(2);
    var textNode = document.createTextNode('');
    cellspacer1.appendChild(textNode);

    // MiddleLeft cell
    var cellMiddleLeft = row.insertCell(3);
    var el = document.createElement('input');
    el.setAttribute('type', 'text');
    el.setAttribute('name', 'PHONE' + iteration);
    el.setAttribute('size', '20');
    el.setAttribute('maxLength', '12');
    el.onkeypress=function(){if ((event.keyCode < 48) || (event.keyCode > 57)) event.returnValue = false;};
    el.onkeyup=function(){return mask(this.value,this,'3,7','-')};
    el.onblur=function(){return mask(this.value,this,'3,7','-')};
    cellMiddleLeft.appendChild(el);
    el.focus();
    //-->
    </SCRIPT> 
    Then make you variables global instead of local declaration, easy eh?
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  3. #3
    Join Date
    Dec 2004
    Location
    canada
    Posts
    53
    Then make you variables global instead of local declaration, easy eh?
    I'm just a noob at Javascript so any help would be appreciated. How would you make the variable global?
    Dave

    ---------------------------------------
    It's the end of the world as we know it, and I feel fine...

  4. #4
    Join Date
    May 2004
    Location
    Manhattan NY
    Posts
    6,028
    Just declare them outside of any function, and they have global scope. Any function can access em then.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles