SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist artcoder's Avatar
    Join Date
    Aug 2005
    Location
    Planet Earth
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    webkit browsers getting Uncaught TypeError

    I got this simple test page linked here that is suppose to automatically "click" a box at load time.

    Works in all browsers, except for Chrome and Safari (webkit browsers).

    I'm getting the error ...

    Code:
    Uncaught TypeError: Object #<HTMLDivElement> has no method 'click'
    Do a "view source" and you will see all the code there as being ...

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <style>
    .box {
    	width: 100px;
    	height: 100px;
    	margin-right: 100px;
    	float:left;
    	background-color:#0099CC;
    }
    </style>
    </head>
    
    <body onload="doit()">
    <div class="box" onclick="alert('box1');">box 1</div>
    <div class="box" onclick="alert('box2');">box 2</div>
    <div class="box" onclick="alert('box3');">box 3</div>
    
    <script>
    function doit() {
    	var randbox=Math.floor(Math.random()*3);
    	var boxes = document.getElementsByClassName('box');
    	boxes[randbox].click();
    }
    	
    	/*
    	if(document.getElementsByClassName){
    		alert('yes i can get');
    	} else {
    		alert('no i can not get element by class name');
    	}
    	*/
    	
    </script>
    </body>
    </html>

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The message means exactly what it says. The click method is not standard, nor in my view should it be.
    In your case you can just call the relevant onclick handler.
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Evangelist artcoder's Avatar
    Join Date
    Aug 2005
    Location
    Planet Earth
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, if I were to use JQuery. What JQuery statement would invoke a "click" on a random box every 3 seconds?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by artcoder View Post
    Okay, if I were to use JQuery. What JQuery statement would invoke a "click" on a random box every 3 seconds?
    You would use setInterval to achieve the timed interval, within which you would use the jQuery .trigger() command to trigger the appropriate event on the element.

    What Logic Ali was trying to get you to realize, I think, is that instead of calling the click() method, you can call the onclick() method of the element which allows you to trigger that appropriate handler for that element.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •