multiple html sections, check what section I am on
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
add a comment |
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
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
add a comment |
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
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
javascript jquery html arrays id
edited Nov 13 '18 at 16:05
Rory McCrossan
243k29211247
243k29211247
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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>
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 thatid
. 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
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 thatid
. 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
add a comment |
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>
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 thatid
. 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
add a comment |
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>
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>
answered Nov 13 '18 at 16:11
mbharanidharan88mbharanidharan88
4,03442454
4,03442454
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 thatid
. 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
add a comment |
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 thatid
. 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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