Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
116
rated 0 times [  118] [ 2]  / answers: 1 / hits: 15633  / 13 Years ago, sat, july 2, 2011, 12:00:00

In Google+, the button that is used to post a comment is made from a div:


<div role="button" id=":1vq.post" class="d-s-r tk3N6e-e tk3N6e-e-qc" aria-disabled="false" style="-webkit-user-select: none; " tabindex="0">Post comment</div>  

I think I can click it with:


document.getElementById(":1vq.post").click(); 

But it says that the element have no attribute click, and I found that onclick is null. So how could I click the button with JavaScript?


More From » dom-events

 Answers
50

EDIT: After a chat with wong2 who started this question and a lot of failed guesses for what this question is really about (the question is quite poorly written), what they are trying to do is to write a Greasemonkey userscript to make the enter key press the Post Comment button in Google+ streams. Since I don't have an invite into Google+ yet, I can't even see the relevant code so not much I can do. This question is not about putting anything related to Google+ in your own site - it's about trying to use Greasemonkey to modify the behavior of Google's site in your own browser.



Earlier attempts to help:



id=:1vq.post is not a legal CSS id name. You can't use the : character in a selector name. This causes multiple issues because not only is it not a legal character, but it's also a meaningful character in the CSS selector syntax. So, I see that you have two issues.



First, your selector logic is not working. Second, as others have said, you can't just assign to click in this way with plain javascript (e.g. no framework).



If you change your selector logic to work correctly, you can get it to work properly (using jQuery) like this:



<div role=button id=aPost class=d-s-r tk3N6e-e tk3N6e-e-qc aria-disabled=false style=-webkit-user-select: none;  tabindex=0>Post comment</div>

$(#aPost).click(function() {
alert(I was clicked.);
});


And, you can see it in action in this jsFiddle: http://jsfiddle.net/jfriend00/Yfnc7/. Click Run and then click on the Post Comment.


[#91383] Friday, July 1, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
margaritakristinak

Total Points: 502
Total Questions: 127
Total Answers: 98

Location: England
Member since Mon, May 17, 2021
3 Years ago
;