0

On a third party site I want to click on the following link to open a modal. <a data-login-modal href=/login class="link-reset">

I was thinking using the .click() method.

Yet I'm in trouble because that <a> :

  • does not have ID - so I can't use $(#id).click();
  • does not have a unique class - so I can't use $(.class).click();

What is the proper way to use the .click() method on the basis of the href or the <a> name (data-login-modal) ?

1
  • Try using code formatting, it's fun! Commented Jul 24, 2015 at 12:52

3 Answers 3

2

To get an element by name in jQuery:

$('[name="ElementNameHere"]').click();

Or more specifically for your requirement:

$('[name="ElementNameHere"]').click(function () {
   // do stuff
});

Example here.

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

1 Comment

Cannot make work either of this two way in the Chrome console.. only the href way proposed by @Rajan works, don't know why..
0

You can use either of the following solutions:

$("a[href='your_href_link']").click(function(){
//body
})

OR

$("a[name='data-login-modal']").click(function(){
//body
})

1 Comment

I'll go with that answer as using the href is the only way I can make it work. The $("a[name='data-login-modal']").click(function(){ //body }) doest not work in the chrome javascript console, it just returns an empty array [ ]
0
$('a[href="your_href_link"]').on('click', function(e){
    e.preventDefault(); // preventing normal href!
});

Without preventDefault(), the href will also be executed!

Further, it's not a nice approach to select based on href. Not really maintainable because links are often dynamic. I suggests you to use id's, classes or data-attributes.

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.