www.webdeveloper.com
Results 1 to 5 of 5

Thread: using eval to run a toggle function

  1. #1
    Join Date
    Jun 2004
    Posts
    1,299

    using eval to run a toggle function

    Hi,

    I have 2 radio buttons on the form that when clicked hides and shows some divs.

    This all works fine. What I am doing though is some form validation for my project and I need to use JS to check the correct box again. when it returns.


    I have the following script that does this without a problem.

    The issue I then have is I want it to be able to show the correct divs again by pulling the information from the onclick attribution and run it.

    Code:
    function setCheckedValue(FormName, InputName, newValue) {
    	
    	var radioObj = document.getElementsByName(InputName);
    	
    	if(!radioObj)
    		return;
    		
    	var radioLength = radioObj.length;
    	
    	if(radioLength == undefined) {
    		radioObj.checked = (radioObj.value == newValue.toString());
    		return;
    	}
    	
    	for(var i = 0; i < radioLength; i++) {
    		radioObj[i].checked = false;
    		if(radioObj[i].value == newValue.toString()) {
    			radioObj[i].checked = true;
    			eval(radioObj.item(i).getAttribute("onclick"));
    		}
    	}
    }
    The attribute is coming back correct which the togglediv('1','2'); however it is not running it and not showing the div.

    I am getting object required on the eval function.

    Any help would be great.

    Thanks
    k0r54

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Have you set it so that upon page load it checks the status of the radio buttons and displays accordingly?

    ^_^

  3. #3
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Event firing:
    Code:
    function fireOnclick(obj) {
    var target=obj;
    if(document.dispatchEvent) { // W3C
        var oEvent = document.createEvent( "MouseEvents" );
        oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target);
        target.dispatchEvent( oEvent );
        }
    else if(document.fireEvent) { // IE
        target.fireEvent("onclick");
        }    
    }
    Code:
    fireOnclick(radioObj[i]);

  4. #4
    Join Date
    Jun 2004
    Posts
    1,299
    ok my code now looks like

    Code:
    // Firefunction
    			function fireOnclick(obj) {
    			  
    			  var target=obj;
    			  
    			  if(document.dispatchEvent) { // W3C
    				var oEvent = document.createEvent( "MouseEvents" );
    				oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target);
    				target.dispatchEvent( oEvent );
    			  } else if(document.fireEvent) { // IE
    				target.fireEvent("onclick");
    			  }    
    			}
    
    
    			// Set checkbox
    			function setRadioValue(InputName, newValue) {
    				
    				var radioObj = document.getElementsByName(InputName);
    				
    				if(!radioObj)
    					return;
    					
    				var radioLength = radioObj.length;
    				
    				if(radioLength == undefined) {
    					radioObj.checked = (radioObj.value == newValue.toString());
    					return;
    				}
    				
    				for(var i = 0; i < radioLength; i++) {
    					radioObj[i].checked = false;
    					if(radioObj[i].value == newValue.toString()) {
    						radioObj[i].checked = true;
    						
    						fireOnclick(radioObj[i]);
    					}
    				}
    			}
    Am I missing something as it is not working?

  5. #5
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Works OK
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
    window.onload=function() {
    var radioObj = document.getElementsByName('fruit');
    for(var i=0; i<radioObj.length; i++) {
        radioObj[i].onclick = function() {alert('fired onclick of ' + this.value);};
        }
    };
    
    // Firefunction
    			function fireOnclick(obj) {
    			  
    			  var target=obj;
    			  
    			  if(document.dispatchEvent) { // W3C
    				var oEvent = document.createEvent( "MouseEvents" );
    				oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target);
    				target.dispatchEvent( oEvent );
    			  } else if(document.fireEvent) { // IE
    				target.fireEvent("onclick");
    			  }    
    			}
    
    
    			// Set checkbox
    			function setRadioValue(InputName, newValue) {
    				
    				var radioObj = document.getElementsByName(InputName);
    				
    				if(!radioObj)
    					return;
    					
    				var radioLength = radioObj.length;
    				
    				if(radioLength == undefined) {
    					radioObj.checked = (radioObj.value == newValue.toString());
    					return;
    				}
    				for(var i = 0; i < radioLength; i++) {
    					radioObj[i].checked = false;
    					if(radioObj[i].value == newValue.toString()) {
    						radioObj[i].checked = true;
    						
    						fireOnclick(radioObj[i]);
    					}
    				}
    			}
    </script>
    
    <style type="text/css">
    * {margin:0;padding:0;}
    </style>
    
    </head>
    <body>
    <form action="#" method="" name="form11">
    <fieldset>
        <label>apple<input type="radio" name="fruit" value="apple"></label>
        <label>banana<input type="radio" name="fruit" value="banana"></label>
        <label>cranberry<input type="radio" name="fruit" value="cranberry" checked></label>
        <button type="button" onclick="setRadioValue('fruit', 'cranberry')">setRadioValue</button>
    </fieldset>
    </form>
    </body>
    </html>

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