multiple html sections, check what section I am on












0















I have 2 HTML sections, they both have the same id (out of my hands, third part cms)



I am currently running some simple code to see how many html section with this ID there is.



How could I set the get array object I am clicking on ?



My js to see how many sections with same ID



so I would like a way that i can console.log(arrayItem) and see what array object i clicked on.



var elms = document.querySelectorAll("[id='sectorpage-strengths']");

for(var i = 0; i < elms.length; i++)

console.log(elms);
console.log(elms[i]);









share|improve this question




















  • 2





    To get the element you're clicking on, add an event listener to it. It should be noted, however, that this is a very hacky solution to a much bigger problem caused by invalid HTML generated from your CMS. That's the issue you should be spending time fixing.

    – Rory McCrossan
    Nov 13 '18 at 16:06













  • yeah, however I cant fix that for now. but its on my list to fix later next year

    – Beep
    Nov 13 '18 at 16:08











  • Please expand upon what you mean by: "How could I set the get array object I am clicking on ?". What is your end goal here.

    – Ben Sewards
    Nov 13 '18 at 16:11
















0















I have 2 HTML sections, they both have the same id (out of my hands, third part cms)



I am currently running some simple code to see how many html section with this ID there is.



How could I set the get array object I am clicking on ?



My js to see how many sections with same ID



so I would like a way that i can console.log(arrayItem) and see what array object i clicked on.



var elms = document.querySelectorAll("[id='sectorpage-strengths']");

for(var i = 0; i < elms.length; i++)

console.log(elms);
console.log(elms[i]);









share|improve this question




















  • 2





    To get the element you're clicking on, add an event listener to it. It should be noted, however, that this is a very hacky solution to a much bigger problem caused by invalid HTML generated from your CMS. That's the issue you should be spending time fixing.

    – Rory McCrossan
    Nov 13 '18 at 16:06













  • yeah, however I cant fix that for now. but its on my list to fix later next year

    – Beep
    Nov 13 '18 at 16:08











  • Please expand upon what you mean by: "How could I set the get array object I am clicking on ?". What is your end goal here.

    – Ben Sewards
    Nov 13 '18 at 16:11














0












0








0








I have 2 HTML sections, they both have the same id (out of my hands, third part cms)



I am currently running some simple code to see how many html section with this ID there is.



How could I set the get array object I am clicking on ?



My js to see how many sections with same ID



so I would like a way that i can console.log(arrayItem) and see what array object i clicked on.



var elms = document.querySelectorAll("[id='sectorpage-strengths']");

for(var i = 0; i < elms.length; i++)

console.log(elms);
console.log(elms[i]);









share|improve this question
















I have 2 HTML sections, they both have the same id (out of my hands, third part cms)



I am currently running some simple code to see how many html section with this ID there is.



How could I set the get array object I am clicking on ?



My js to see how many sections with same ID



so I would like a way that i can console.log(arrayItem) and see what array object i clicked on.



var elms = document.querySelectorAll("[id='sectorpage-strengths']");

for(var i = 0; i < elms.length; i++)

console.log(elms);
console.log(elms[i]);






javascript jquery html arrays id






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 16:05









Rory McCrossan

242k29207245




242k29207245










asked Nov 13 '18 at 16:05









BeepBeep

1,59042153




1,59042153








  • 2





    To get the element you're clicking on, add an event listener to it. It should be noted, however, that this is a very hacky solution to a much bigger problem caused by invalid HTML generated from your CMS. That's the issue you should be spending time fixing.

    – Rory McCrossan
    Nov 13 '18 at 16:06













  • yeah, however I cant fix that for now. but its on my list to fix later next year

    – Beep
    Nov 13 '18 at 16:08











  • Please expand upon what you mean by: "How could I set the get array object I am clicking on ?". What is your end goal here.

    – Ben Sewards
    Nov 13 '18 at 16:11














  • 2





    To get the element you're clicking on, add an event listener to it. It should be noted, however, that this is a very hacky solution to a much bigger problem caused by invalid HTML generated from your CMS. That's the issue you should be spending time fixing.

    – Rory McCrossan
    Nov 13 '18 at 16:06













  • yeah, however I cant fix that for now. but its on my list to fix later next year

    – Beep
    Nov 13 '18 at 16:08











  • Please expand upon what you mean by: "How could I set the get array object I am clicking on ?". What is your end goal here.

    – Ben Sewards
    Nov 13 '18 at 16:11








2




2





To get the element you're clicking on, add an event listener to it. It should be noted, however, that this is a very hacky solution to a much bigger problem caused by invalid HTML generated from your CMS. That's the issue you should be spending time fixing.

– Rory McCrossan
Nov 13 '18 at 16:06







To get the element you're clicking on, add an event listener to it. It should be noted, however, that this is a very hacky solution to a much bigger problem caused by invalid HTML generated from your CMS. That's the issue you should be spending time fixing.

– Rory McCrossan
Nov 13 '18 at 16:06















yeah, however I cant fix that for now. but its on my list to fix later next year

– Beep
Nov 13 '18 at 16:08





yeah, however I cant fix that for now. but its on my list to fix later next year

– Beep
Nov 13 '18 at 16:08













Please expand upon what you mean by: "How could I set the get array object I am clicking on ?". What is your end goal here.

– Ben Sewards
Nov 13 '18 at 16:11





Please expand upon what you mean by: "How could I set the get array object I am clicking on ?". What is your end goal here.

– Ben Sewards
Nov 13 '18 at 16:11












1 Answer
1






active

oldest

votes


















1














You can try something like this and get the this object






$('[id="sectorpage-strengths"]').click(function(){

var obj = $(this).text();

console.log("Click Event Triggered By " + obj);

});

section{
border: solid 1px red;
width: 100px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="sectorpage-strengths">Section 1</section>
<section id="sectorpage-strengths">Section 2</section>








share|improve this answer



















  • 1





    That's the cookie, perfect thank you

    – Beep
    Nov 13 '18 at 16:13











  • The selector will only select the first element that matches the id attribute. update: I was incorrect, this is only the case when you directly use an ID Selector and not the attribute selector

    – Ben Sewards
    Nov 13 '18 at 16:13













  • @BenSewards, It will selects all the matching element and it is working. Please click on sections

    – mbharanidharan88
    Nov 13 '18 at 16:14











  • @BenSewards as this uses the attribute it will select all elements with that id. It's still far from a workable solution though

    – Rory McCrossan
    Nov 13 '18 at 16:14






  • 1





    good point, did not realize this was using attribute selector and not directly jquery #id selector

    – Ben Sewards
    Nov 13 '18 at 16:15











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284970%2fmultiple-html-sections-check-what-section-i-am-on%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can try something like this and get the this object






$('[id="sectorpage-strengths"]').click(function(){

var obj = $(this).text();

console.log("Click Event Triggered By " + obj);

});

section{
border: solid 1px red;
width: 100px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="sectorpage-strengths">Section 1</section>
<section id="sectorpage-strengths">Section 2</section>








share|improve this answer



















  • 1





    That's the cookie, perfect thank you

    – Beep
    Nov 13 '18 at 16:13











  • The selector will only select the first element that matches the id attribute. update: I was incorrect, this is only the case when you directly use an ID Selector and not the attribute selector

    – Ben Sewards
    Nov 13 '18 at 16:13













  • @BenSewards, It will selects all the matching element and it is working. Please click on sections

    – mbharanidharan88
    Nov 13 '18 at 16:14











  • @BenSewards as this uses the attribute it will select all elements with that id. It's still far from a workable solution though

    – Rory McCrossan
    Nov 13 '18 at 16:14






  • 1





    good point, did not realize this was using attribute selector and not directly jquery #id selector

    – Ben Sewards
    Nov 13 '18 at 16:15
















1














You can try something like this and get the this object






$('[id="sectorpage-strengths"]').click(function(){

var obj = $(this).text();

console.log("Click Event Triggered By " + obj);

});

section{
border: solid 1px red;
width: 100px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="sectorpage-strengths">Section 1</section>
<section id="sectorpage-strengths">Section 2</section>








share|improve this answer



















  • 1





    That's the cookie, perfect thank you

    – Beep
    Nov 13 '18 at 16:13











  • The selector will only select the first element that matches the id attribute. update: I was incorrect, this is only the case when you directly use an ID Selector and not the attribute selector

    – Ben Sewards
    Nov 13 '18 at 16:13













  • @BenSewards, It will selects all the matching element and it is working. Please click on sections

    – mbharanidharan88
    Nov 13 '18 at 16:14











  • @BenSewards as this uses the attribute it will select all elements with that id. It's still far from a workable solution though

    – Rory McCrossan
    Nov 13 '18 at 16:14






  • 1





    good point, did not realize this was using attribute selector and not directly jquery #id selector

    – Ben Sewards
    Nov 13 '18 at 16:15














1












1








1







You can try something like this and get the this object






$('[id="sectorpage-strengths"]').click(function(){

var obj = $(this).text();

console.log("Click Event Triggered By " + obj);

});

section{
border: solid 1px red;
width: 100px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="sectorpage-strengths">Section 1</section>
<section id="sectorpage-strengths">Section 2</section>








share|improve this answer













You can try something like this and get the this object






$('[id="sectorpage-strengths"]').click(function(){

var obj = $(this).text();

console.log("Click Event Triggered By " + obj);

});

section{
border: solid 1px red;
width: 100px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="sectorpage-strengths">Section 1</section>
<section id="sectorpage-strengths">Section 2</section>








$('[id="sectorpage-strengths"]').click(function(){

var obj = $(this).text();

console.log("Click Event Triggered By " + obj);

});

section{
border: solid 1px red;
width: 100px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="sectorpage-strengths">Section 1</section>
<section id="sectorpage-strengths">Section 2</section>





$('[id="sectorpage-strengths"]').click(function(){

var obj = $(this).text();

console.log("Click Event Triggered By " + obj);

});

section{
border: solid 1px red;
width: 100px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="sectorpage-strengths">Section 1</section>
<section id="sectorpage-strengths">Section 2</section>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 16:11









mbharanidharan88mbharanidharan88

4,03932454




4,03932454








  • 1





    That's the cookie, perfect thank you

    – Beep
    Nov 13 '18 at 16:13











  • The selector will only select the first element that matches the id attribute. update: I was incorrect, this is only the case when you directly use an ID Selector and not the attribute selector

    – Ben Sewards
    Nov 13 '18 at 16:13













  • @BenSewards, It will selects all the matching element and it is working. Please click on sections

    – mbharanidharan88
    Nov 13 '18 at 16:14











  • @BenSewards as this uses the attribute it will select all elements with that id. It's still far from a workable solution though

    – Rory McCrossan
    Nov 13 '18 at 16:14






  • 1





    good point, did not realize this was using attribute selector and not directly jquery #id selector

    – Ben Sewards
    Nov 13 '18 at 16:15














  • 1





    That's the cookie, perfect thank you

    – Beep
    Nov 13 '18 at 16:13











  • The selector will only select the first element that matches the id attribute. update: I was incorrect, this is only the case when you directly use an ID Selector and not the attribute selector

    – Ben Sewards
    Nov 13 '18 at 16:13













  • @BenSewards, It will selects all the matching element and it is working. Please click on sections

    – mbharanidharan88
    Nov 13 '18 at 16:14











  • @BenSewards as this uses the attribute it will select all elements with that id. It's still far from a workable solution though

    – Rory McCrossan
    Nov 13 '18 at 16:14






  • 1





    good point, did not realize this was using attribute selector and not directly jquery #id selector

    – Ben Sewards
    Nov 13 '18 at 16:15








1




1





That's the cookie, perfect thank you

– Beep
Nov 13 '18 at 16:13





That's the cookie, perfect thank you

– Beep
Nov 13 '18 at 16:13













The selector will only select the first element that matches the id attribute. update: I was incorrect, this is only the case when you directly use an ID Selector and not the attribute selector

– Ben Sewards
Nov 13 '18 at 16:13







The selector will only select the first element that matches the id attribute. update: I was incorrect, this is only the case when you directly use an ID Selector and not the attribute selector

– Ben Sewards
Nov 13 '18 at 16:13















@BenSewards, It will selects all the matching element and it is working. Please click on sections

– mbharanidharan88
Nov 13 '18 at 16:14





@BenSewards, It will selects all the matching element and it is working. Please click on sections

– mbharanidharan88
Nov 13 '18 at 16:14













@BenSewards as this uses the attribute it will select all elements with that id. It's still far from a workable solution though

– Rory McCrossan
Nov 13 '18 at 16:14





@BenSewards as this uses the attribute it will select all elements with that id. It's still far from a workable solution though

– Rory McCrossan
Nov 13 '18 at 16:14




1




1





good point, did not realize this was using attribute selector and not directly jquery #id selector

– Ben Sewards
Nov 13 '18 at 16:15





good point, did not realize this was using attribute selector and not directly jquery #id selector

– Ben Sewards
Nov 13 '18 at 16:15


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284970%2fmultiple-html-sections-check-what-section-i-am-on%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine