supriya
supriya

Reputation: 31

How to call same JQuery function on click of elements having same ids

I Have many elements having same is but with different data,I want to call same JQuery function on click of each element.How can I do this?

Upvotes: 3

Views: 5915

Answers (4)

ankur
ankur

Reputation: 4733

When operating with id's can create lot of redundancy it is better to operate with classes but if your classes are getting repeated than it could be a bit tricky to handle i would recommend you to use some custom attributes for this

Some sample code for you.

    var spnActive = document.createElement("span");
    $(spnActive).attr('isExpColSpan', 'true');
    $('#dvContainer').find('span[isExpColSpan="true"]').

In the second line i have added an custom attribute in the next line i have shown how to use this attribute.

Upvotes: 0

xkeshav
xkeshav

Reputation: 54016

try with

jQuery ("div,span,p.myClass").click(function() {

//your code
});

Reference

Upvotes: 1

paztulio
paztulio

Reputation: 351

Using the same ID on more than one element is not allowed. Use classes for that.

<div class="myClass">..</div>
<div class="myClass">..</div>

$(".myClass").click(function() {...});

Upvotes: 6

jAndy
jAndy

Reputation: 235972

You cannot have multiple ID's in your HTML markup.

It would be an invalid markup. When querying for $('#foobar') and there are five elements which have that id, you would only get the first instance. So even if there would be a way (...) to apply code to all of those nodes, don't do it.

Use Classnames when you want to "combine" some elements.

Upvotes: 8

Related Questions