0

I need to capture click on elements with ids stored in array ids.

E.g:

ids = ['item1','item3','item4']

<a href='' id='item1'></a>
<a href='' id='item3'></a>
<a href='' id='item4'></a>

When clicking on 3 it should return 3 and prevent redirect.

3
  • hey man - you got any jsfiddle? cheers Commented Mar 17, 2012 at 22:43
  • 1
    You cannot have IDs that begin with a digit. w3.org/TR/html4/types.html#type-id Commented Mar 17, 2012 at 22:49
  • What do you mean by "return 3" what is the DOM structure? did my answer helped you? Commented Mar 17, 2012 at 22:55

1 Answer 1

2

If return 3 means alert 3...

$.each(ids, function(index, value) {
    $('#' + value).click(function(event) {
        alert(this.id);
        event.preventDefault();
    });
});​

Live DEMO

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

2 Comments

@ElliotBonneville. Easy question, typo... :)
Could you upload a fiddle? For some reason I cannot make it work.

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.