1

Here is javascript code.

document.querySelectorAll("li[class='user selected']")

I wanna click on this element but this code does not work

document.querySelectorAll("li[class='user selected']").click();

How can I click on this element ? Thanks.

Edit : this is Html code

<ul class="search" id="typeahead_list_u_0_2" role="listbox">
<li class="user selected" title="Special Username" aria-label="Special Username" role="option" aria-selected="true" id="js_g">
<a href="https://..." rel="ignore" target=""><img alt="" src="https://...">
<span class="text">Special Username</span>
</a>
</li>
</ul>
7
  • querySelectorAll will return you array of selected element. Not the first match. Use querySelector instead Commented May 4, 2015 at 13:09
  • Actually it works fine when I want to return first element but the problem is click event, I can not click . Commented May 4, 2015 at 13:12
  • If you add HTML code, i can have a look at it and can suggest. Try to put the code in fiddle and share it. Commented May 4, 2015 at 13:13
  • I have updated html code, do you want me to add more html codes ? Commented May 4, 2015 at 13:40
  • This is good enough. So, at time, you will have only one li element with class user selected ? Or many li elements will have that class ? Commented May 4, 2015 at 13:43

2 Answers 2

2

Remove the sample click listener that i have attached.

var element = document.querySelector("li[class='user selected']");

if (element) {
    
    // TODO: Attaching sample click listener. Remove it.
    element.addEventListener('click', function () {
        alert('Clicked');
    }, false);
    
    element.click();

}
<li class="user selected" title="Special User Name" aria-label="Special User Name" role="option" aria-selected="true" id="js_x"></li>

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your effort, But it does not work , I do not know what is wrong.
How are you attaching click listener ?
I have updated my html code again, maybe something is wrong with my code
So, you are trying to redirect the user to the URL specified in href of anchor tag ?
I am trying to add my friends into a fb group by code. I type name automatically but I can not pick or click on element.
1

Have you tried ?

document.querySelectorAll("li[class='user selected']")[0].click();

Will click on the first ([0]) li[class='user selected']

1 Comment

yeah I have tried, It does not work. When I try this code I get an error message "Uncaught TypeError: Cannot read property '0' of nullmessage: "Cannot read property '0' of null"stack: (...)get stack: function () { [native code] }set stack: function () { [native code] }__proto__: Error(anonymous function) ..."

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.