2

I know the following function to trigger click:

var link = $("#linkId");
    link.click();

but in my case I don`t have id, but I have name attribute, so I used:

var link = $('a[name=content_'+params['p']+']');
link.click();

But it is not clicking, what is the problem with the code above. if I use id it works fine.

3
  • 1
    Your selector might be incorrect. Make sure the result of 'a[name=content_'+params['p']+']' refers to an existing element. Also, calling .click will only execute the event handlers, it will not make the browser follow that link. Commented Aug 13, 2012 at 11:18
  • 1
    @FelixKling Only because jQuery has a special condition on not calling the native method .click() specifically on a elements. jsfiddle.net/HB6X5/5 :P It does call it on other types of element (the native .click() method) if the default action wasn't prevented and the target element has the native method. Commented Aug 13, 2012 at 11:29
  • @Esailija: Ha, learned something new :) Thanks a lot! Commented Aug 13, 2012 at 11:32

3 Answers 3

2

I'm assuming you actually have a click event bound to the matching link. If so...

With attribute selectors, if the value is anything more complex than a sequence of letters (e.g. includes underscores or other punctuation chars), enclose it in quotes.

var link = $('a[name="content_'+params['p']+'"]');
Sign up to request clarification or add additional context in comments.

Comments

0

make sure you are getting the element by your selector try to debug it
$("[attributename='value']") will you give you the correct element

Comments

0

You can't trigger a click to an anchor tag by program until you bind click event to that.

You can do:

var link = $('a[name="content_'+params['p']+'"]'); // change the selector with encloses quote
link.click(function() {
  // do  something
});

Then trigger that click event by:

link.click();

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.