last day (15 days later) » 

07:10
-1
Q: Jquery .click() not working for an anchor

Vishal KumarI am trying to trigger click event for an anchor tag programmatically using jquery, but unable to do so. The anchor tag is placed in that file statically and not being generated dynamically. I have wrapped the code into $(document).ready. Console.log is perfectly logging the element into the con...

What you suppose to be happening on its click? There doesnt seem to be any click event handler written in the code.
Use developer tools(Press F12) & reload the page. See under console if there are any errors. Is that ID(edit-desc) unique in your DOM?
the code works. Can you elaborate the exact issue
@techie_28 No, there is no error, only the element being logged by console.log()
Try replacing the hyphen with an underscore throughout. Code seems to have no error & should be working.
Are you trying to trigger('click')?
07:10
@Satpal This is just a small piece of code, its working here but not on the actual setup.
@VishalKumar you perhaps have many anchor tags with the same ID i.e edit-desc?
@JohnPete22 Yes I am
@techie_28 console.log($("#edit-desc")) is logging matched element length 1
Well you're basically setting the click event with .on('click', function) and then you're setting it again with .click(). If you want to trigger the button, you gotta use .trigger('click')
@VishalKumar if you have multiple items with same id then at first you should make changes in html and make ids unique, if you want to attach event handler on multiple elements then consider using class instead of id
@JohnPete22 I think calling the .click() without params should invoke the event
07:10
@JohnPete22 I think it does not work like that, anyways I have tried using trigger() too
would help if you can add html code sample as well
@Vikram Thanks, but I am sure there is only one item with that ID
@VishalKumar In case you are expecting it to work on many elements you should be using a class selector instead of an ID one. i.e .edit-desc not #edit-desc provided all of the intended elements do have that class.
There seem to be some other problem/error in your code because here in SO the alert is showing fine when the code is tried using the Run Code Snippet button.
@VishalKumar then I would suggest to add inline click handler to check if it works w3schools.com/jsref/met_html_click.asp
@VishalKumar hope you havent disabled alert popups from browser. Try replacing the alert with a console.log statement.
07:10
@techie_28 alerts are enabled, works well when I manually click on the element
It works fine here on Stack Overflow too through the Run Code Snippet button. It wouldnt have if there was a problem.
@techie_28 hmm...Let's see If I could figure this out, thanks everyone for your time :)

last day (15 days later) »