1

I use $('div[@click="login()"]') to select

<div @click="login()"> Login </div>

but it gives

VM353:1 Uncaught DOMException: Failed to execute '$' on 'CommandLineAPI': 'div[@click="login()"]' is not a valid selector.
    at <anonymous>:1:1

what is the correct way?

5
  • I don't think that's valid css. Commented Dec 5, 2022 at 1:14
  • Can you give it another, valid selector? Commented Dec 5, 2022 at 1:15
  • @chovy if it's a regular css then it would be easy, i won't ask question here :-P Commented Dec 5, 2022 at 1:24
  • @fubar no, xpath is not possible because i found the xpath to the element is not fixed. that's why i tried css selector but it's not valid. if querySelector is impossible may be i have to use innerText? Commented Dec 5, 2022 at 1:27
  • can't you add a class or id to it? Commented Dec 5, 2022 at 6:38

1 Answer 1

1

That's not valid HTML.

You commented on another answer with links to "this, this and this". Those are all questions about vue.js, a templating language that transpiles to (produces) HTML. Other templating languages include EJS, Pug, Handlebars, React, and Angular.

If you are really using vue.js in your project, you should add the tag to this question and future questions related to it.

The @click attribute is only meaningful to Vue, and it will not be present in the actual HTML on the webpage. Vue might turn it into an onclick handler, so you could try using $('div[onclick="login()"]') which might return you the element you want.

Otherwise, give your div an id too:

<div id="login-button" @click="login()"> Login </div>

And select on that id: $('#login-button')

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

1 Comment

It's "invalid" only because <html:DIV> doesn't have a declared @click attribute, but the same would be true for the click attribute. The @ character doesn't make it invalid HTML. <my-div @click="login()"></my-div> is perfectly valid HTML (even though it won't be serializable as XML 1.0)

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.