Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm new to JQuery/Javascript etc... based on the following article:
How to make an anchor tag refer to nothing?
I would like to apply the java function to several id's. Can we not make the function execute for classes as opposed to ids?
<span class="style1" id="myid">Link</span>
<span class="style1" id="myid">Link</span>
<span class="style1" id="myid">Link</span>
<span class="style1" id="myid">Link</span>
<span class="style1" id="myid">Link</span>
$('myid').click(function() {
/* put your code here */
Basically as above, how do I execute the function above for ALL of the links? Is this possible? Thanks in advance.
–
<span class="style1" id="myid1">Link</span>
<span class="style1" id="myid2">Link</span>
<span class="style1" id="myid3">Link</span>
<span class="style1" id="myid4">Link</span>
<span class="style1" id="myid5">Link</span>
then use this code
$('#myid1,#myid2,#myid3,#myid4,#myid5').click(function() {
/* put your code here */
–
–
First off, IDs should be unique. You should not have multiple elements with the same ID.
To select by ID in jQuery use the #
character. $('#myid')
. This will get the first element with that ID, as there should only be one (you can kinda cheat by doing $('[id="myid"]')
to get get multiple elements with the same ID).
I suggest using a class to select all of your links. Classes are selected using the .
character.
$('.style1').click(function(){});
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.