6

jquery attaches a click method on even things that aren't typically clickable like a DIV. this can be great because some things respond to that. How can i "click" on any old regular DIV using plain old javascript without Jquery?

what i am trying to do is trigger clicking on the area where the user can post in Jquery. I'm using a chrome extension that can run some javascript on a hotkey. I wrote a jquery version var x = $('div[guidedhelpid="sharebox"]'); x.click();

which works fine, other than it seems that the jquery library only loads about 1/4 of the time. not sure why, so i figured i'd try to make it in plain JS. As for the handler, googles own code is intercepting and processing the clicks fine, and it works in the Jquery version. so i just want to effectively do the same thing.

does Jquery internally go up or down the DOM until it finds the first think clickable?

update in light of it, i was looking in the wrong direction, and really just needed to find the right element to focus on (in JQUERY).

4
  • does Jquery internally go up or down the DOM until it finds the first think clickable? Commented Feb 9, 2012 at 23:28
  • 2
    I'd be more concerned about why jQuery is only loading ¼ of the time. Commented Feb 9, 2012 at 23:36
  • ok i was wrong, jquery was loading. its just the content was different on the page 1/4 of the time. so this question is pretty irrelevant, and the way i worded it originally makes it seem like i'm asking something different than i intended. Commented Feb 10, 2012 at 0:30
  • instead of clicking, its just a matter for me to find the right element to FOCUS on. Commented Feb 10, 2012 at 0:30

5 Answers 5

9

If you just want to bind one function to the element, you could use

var element = document.getElementById("el"); //grab the element
element.onclick = function() { //asign a function
//code
}

but if you need to attach more than one event, you should use addEventListener (eveything except IE) and attachEvent (IE)

if(element.addEventListener) {
element.addEventListener("click", function() {});
} else { 
element.attachEvent("onclick", function() {})
}
Sign up to request clarification or add additional context in comments.

Comments

9

The click() function is built in JavaScript, not jQuery.

HTMLElementObject.click()

Source.

2 Comments

If you're talking about attaching event handlers, the click() in $('div').click(...) is most definitely a jQuery function.
Nope, he says he wants to trigger a click.
0
var d = document.getElementById("test");
d.onclick = function(){alert('hi');};

Comments

-1
var divElement = document.getElementById('section');

divElement.addEventListener('click', function () {
  alert('div clicked');
},false);

Comments

-2

I use tables to control the page layout. The "td" are clickable, so I write my "onclick = whatever()" in the td tag and not in the div tag.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.