Get variable inside certain div with jquery, when there are multiple divs with same id
I'm printing an arraylist with jsp. Each object inside of that arraylist is printed with a loop like this:
<% ArrayList <MessageObject> list = (ArrayList<MessageObject>) request.getAttribute("list"); %>
<%int index = 0;%>
<%for(MessageObject msg :list){
index++;
if(mensaje.getState().compareTo("unread") == 0){%>
<tr data-status="unread" class="unread">
<td>
<a href="javascript:;" class="star">
<i class="glyphicon glyphicon-star"></i>
</a>
</td>
<td>
<div class="media">
<h4 class="title">
User Identifier
</h4>
</div>
</td>
<td id="unread-id">
<div class="media">
<p class="summary"><% out.print(msg.getMessage());%></p>
<input id="index" type="text" value="<%out.print(index);%>"></input>
</div>
Some of the closing tags and other structures are not written above, in order to make my code easier to read.
Basically that prints me messages from a queue, and its index in the arraylist:
My problem is that I want to save the index value of any of my messages when I click on them.
I tried this:
<script>
$(document).on('click', '#unread-id', function () {
var index = $('#index').val();
$("#setindex").val(index);
});
So I click on any div containing a message, the script is called, but I always get the same index value, 1.
Problem is that having always the same div with the same id name, causes that my script always selects the first div with id unread-id, which is always the first one, so it returns 1.
How can I get the index of the clicked div, if all my container divs have the same id value?
javascript java jquery spring jsp
add a comment |
I'm printing an arraylist with jsp. Each object inside of that arraylist is printed with a loop like this:
<% ArrayList <MessageObject> list = (ArrayList<MessageObject>) request.getAttribute("list"); %>
<%int index = 0;%>
<%for(MessageObject msg :list){
index++;
if(mensaje.getState().compareTo("unread") == 0){%>
<tr data-status="unread" class="unread">
<td>
<a href="javascript:;" class="star">
<i class="glyphicon glyphicon-star"></i>
</a>
</td>
<td>
<div class="media">
<h4 class="title">
User Identifier
</h4>
</div>
</td>
<td id="unread-id">
<div class="media">
<p class="summary"><% out.print(msg.getMessage());%></p>
<input id="index" type="text" value="<%out.print(index);%>"></input>
</div>
Some of the closing tags and other structures are not written above, in order to make my code easier to read.
Basically that prints me messages from a queue, and its index in the arraylist:
My problem is that I want to save the index value of any of my messages when I click on them.
I tried this:
<script>
$(document).on('click', '#unread-id', function () {
var index = $('#index').val();
$("#setindex").val(index);
});
So I click on any div containing a message, the script is called, but I always get the same index value, 1.
Problem is that having always the same div with the same id name, causes that my script always selects the first div with id unread-id, which is always the first one, so it returns 1.
How can I get the index of the clicked div, if all my container divs have the same id value?
javascript java jquery spring jsp
add a comment |
I'm printing an arraylist with jsp. Each object inside of that arraylist is printed with a loop like this:
<% ArrayList <MessageObject> list = (ArrayList<MessageObject>) request.getAttribute("list"); %>
<%int index = 0;%>
<%for(MessageObject msg :list){
index++;
if(mensaje.getState().compareTo("unread") == 0){%>
<tr data-status="unread" class="unread">
<td>
<a href="javascript:;" class="star">
<i class="glyphicon glyphicon-star"></i>
</a>
</td>
<td>
<div class="media">
<h4 class="title">
User Identifier
</h4>
</div>
</td>
<td id="unread-id">
<div class="media">
<p class="summary"><% out.print(msg.getMessage());%></p>
<input id="index" type="text" value="<%out.print(index);%>"></input>
</div>
Some of the closing tags and other structures are not written above, in order to make my code easier to read.
Basically that prints me messages from a queue, and its index in the arraylist:
My problem is that I want to save the index value of any of my messages when I click on them.
I tried this:
<script>
$(document).on('click', '#unread-id', function () {
var index = $('#index').val();
$("#setindex").val(index);
});
So I click on any div containing a message, the script is called, but I always get the same index value, 1.
Problem is that having always the same div with the same id name, causes that my script always selects the first div with id unread-id, which is always the first one, so it returns 1.
How can I get the index of the clicked div, if all my container divs have the same id value?
javascript java jquery spring jsp
I'm printing an arraylist with jsp. Each object inside of that arraylist is printed with a loop like this:
<% ArrayList <MessageObject> list = (ArrayList<MessageObject>) request.getAttribute("list"); %>
<%int index = 0;%>
<%for(MessageObject msg :list){
index++;
if(mensaje.getState().compareTo("unread") == 0){%>
<tr data-status="unread" class="unread">
<td>
<a href="javascript:;" class="star">
<i class="glyphicon glyphicon-star"></i>
</a>
</td>
<td>
<div class="media">
<h4 class="title">
User Identifier
</h4>
</div>
</td>
<td id="unread-id">
<div class="media">
<p class="summary"><% out.print(msg.getMessage());%></p>
<input id="index" type="text" value="<%out.print(index);%>"></input>
</div>
Some of the closing tags and other structures are not written above, in order to make my code easier to read.
Basically that prints me messages from a queue, and its index in the arraylist:
My problem is that I want to save the index value of any of my messages when I click on them.
I tried this:
<script>
$(document).on('click', '#unread-id', function () {
var index = $('#index').val();
$("#setindex").val(index);
});
So I click on any div containing a message, the script is called, but I always get the same index value, 1.
Problem is that having always the same div with the same id name, causes that my script always selects the first div with id unread-id, which is always the first one, so it returns 1.
How can I get the index of the clicked div, if all my container divs have the same id value?
javascript java jquery spring jsp
javascript java jquery spring jsp
asked Nov 12 '18 at 22:41
Keka Bron
919
919
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Add a class to your <td id="unread-id">
like row
and change your script for the following one. Your td
should end up looking like <td class="row">
. Also, don't use ids in your inputs, change it to a class, like row-input
.
JS
$(document).on('click', '.row', function () {
var index = $(this).find('.row-input').val();
$("#setindex").val(index);
});
JSP Changes
<td id="unread-id">
to<td class="row">
<input id="index" type="text" value="<%out.print(index);%>"></input>
to<input class="row-input" type="text" value="<%out.print(index);%>"></input>
Note
You are setting the same id
to all your rows. An id must be unique and that is the reason you keep getting the same index.
add a comment |
First - id should be unique in your page. You should really fix this (and if you need some selector to work with multiple elements - you can use classname instead).
However - your code can work (might cause issues with some browsers, so I really advise you to fix this asap):
$(function() {
$(document).on('click', '#unread-id', function () {
console.log($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="unread-id" value="1" /><br />
<input id="unread-id" value="2" /><br />
<input id="unread-id" value="3" /><br />
<input id="unread-id" value="4" /><br />
When inside the click
function - the this
element is the element you just clicked. You can use that in order to get the value that you need.
Well,id
should be unique. That is already a bit of an issue.
– Alain Cruz
Nov 13 '18 at 0:31
1
@AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
– Dekel
Nov 13 '18 at 0:33
and btw - your usage if$(this)
after you read my answer is not so nice :)
– Dekel
Nov 13 '18 at 0:36
You know, I only saw the first duplicated id in histd
, until I realized there was another in hisinput
hahaha :-)
– Alain Cruz
Nov 13 '18 at 0:37
1
Well I am upvoting, because you did show it. Still, yours is missing the td'sid
issue, which he/she should fix.
– Alain Cruz
Nov 13 '18 at 0:40
|
show 2 more comments
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%2f53271142%2fget-variable-inside-certain-div-with-jquery-when-there-are-multiple-divs-with-s%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Add a class to your <td id="unread-id">
like row
and change your script for the following one. Your td
should end up looking like <td class="row">
. Also, don't use ids in your inputs, change it to a class, like row-input
.
JS
$(document).on('click', '.row', function () {
var index = $(this).find('.row-input').val();
$("#setindex").val(index);
});
JSP Changes
<td id="unread-id">
to<td class="row">
<input id="index" type="text" value="<%out.print(index);%>"></input>
to<input class="row-input" type="text" value="<%out.print(index);%>"></input>
Note
You are setting the same id
to all your rows. An id must be unique and that is the reason you keep getting the same index.
add a comment |
Add a class to your <td id="unread-id">
like row
and change your script for the following one. Your td
should end up looking like <td class="row">
. Also, don't use ids in your inputs, change it to a class, like row-input
.
JS
$(document).on('click', '.row', function () {
var index = $(this).find('.row-input').val();
$("#setindex").val(index);
});
JSP Changes
<td id="unread-id">
to<td class="row">
<input id="index" type="text" value="<%out.print(index);%>"></input>
to<input class="row-input" type="text" value="<%out.print(index);%>"></input>
Note
You are setting the same id
to all your rows. An id must be unique and that is the reason you keep getting the same index.
add a comment |
Add a class to your <td id="unread-id">
like row
and change your script for the following one. Your td
should end up looking like <td class="row">
. Also, don't use ids in your inputs, change it to a class, like row-input
.
JS
$(document).on('click', '.row', function () {
var index = $(this).find('.row-input').val();
$("#setindex").val(index);
});
JSP Changes
<td id="unread-id">
to<td class="row">
<input id="index" type="text" value="<%out.print(index);%>"></input>
to<input class="row-input" type="text" value="<%out.print(index);%>"></input>
Note
You are setting the same id
to all your rows. An id must be unique and that is the reason you keep getting the same index.
Add a class to your <td id="unread-id">
like row
and change your script for the following one. Your td
should end up looking like <td class="row">
. Also, don't use ids in your inputs, change it to a class, like row-input
.
JS
$(document).on('click', '.row', function () {
var index = $(this).find('.row-input').val();
$("#setindex").val(index);
});
JSP Changes
<td id="unread-id">
to<td class="row">
<input id="index" type="text" value="<%out.print(index);%>"></input>
to<input class="row-input" type="text" value="<%out.print(index);%>"></input>
Note
You are setting the same id
to all your rows. An id must be unique and that is the reason you keep getting the same index.
edited Nov 13 '18 at 0:35
answered Nov 13 '18 at 0:29
Alain Cruz
1,7381818
1,7381818
add a comment |
add a comment |
First - id should be unique in your page. You should really fix this (and if you need some selector to work with multiple elements - you can use classname instead).
However - your code can work (might cause issues with some browsers, so I really advise you to fix this asap):
$(function() {
$(document).on('click', '#unread-id', function () {
console.log($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="unread-id" value="1" /><br />
<input id="unread-id" value="2" /><br />
<input id="unread-id" value="3" /><br />
<input id="unread-id" value="4" /><br />
When inside the click
function - the this
element is the element you just clicked. You can use that in order to get the value that you need.
Well,id
should be unique. That is already a bit of an issue.
– Alain Cruz
Nov 13 '18 at 0:31
1
@AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
– Dekel
Nov 13 '18 at 0:33
and btw - your usage if$(this)
after you read my answer is not so nice :)
– Dekel
Nov 13 '18 at 0:36
You know, I only saw the first duplicated id in histd
, until I realized there was another in hisinput
hahaha :-)
– Alain Cruz
Nov 13 '18 at 0:37
1
Well I am upvoting, because you did show it. Still, yours is missing the td'sid
issue, which he/she should fix.
– Alain Cruz
Nov 13 '18 at 0:40
|
show 2 more comments
First - id should be unique in your page. You should really fix this (and if you need some selector to work with multiple elements - you can use classname instead).
However - your code can work (might cause issues with some browsers, so I really advise you to fix this asap):
$(function() {
$(document).on('click', '#unread-id', function () {
console.log($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="unread-id" value="1" /><br />
<input id="unread-id" value="2" /><br />
<input id="unread-id" value="3" /><br />
<input id="unread-id" value="4" /><br />
When inside the click
function - the this
element is the element you just clicked. You can use that in order to get the value that you need.
Well,id
should be unique. That is already a bit of an issue.
– Alain Cruz
Nov 13 '18 at 0:31
1
@AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
– Dekel
Nov 13 '18 at 0:33
and btw - your usage if$(this)
after you read my answer is not so nice :)
– Dekel
Nov 13 '18 at 0:36
You know, I only saw the first duplicated id in histd
, until I realized there was another in hisinput
hahaha :-)
– Alain Cruz
Nov 13 '18 at 0:37
1
Well I am upvoting, because you did show it. Still, yours is missing the td'sid
issue, which he/she should fix.
– Alain Cruz
Nov 13 '18 at 0:40
|
show 2 more comments
First - id should be unique in your page. You should really fix this (and if you need some selector to work with multiple elements - you can use classname instead).
However - your code can work (might cause issues with some browsers, so I really advise you to fix this asap):
$(function() {
$(document).on('click', '#unread-id', function () {
console.log($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="unread-id" value="1" /><br />
<input id="unread-id" value="2" /><br />
<input id="unread-id" value="3" /><br />
<input id="unread-id" value="4" /><br />
When inside the click
function - the this
element is the element you just clicked. You can use that in order to get the value that you need.
First - id should be unique in your page. You should really fix this (and if you need some selector to work with multiple elements - you can use classname instead).
However - your code can work (might cause issues with some browsers, so I really advise you to fix this asap):
$(function() {
$(document).on('click', '#unread-id', function () {
console.log($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="unread-id" value="1" /><br />
<input id="unread-id" value="2" /><br />
<input id="unread-id" value="3" /><br />
<input id="unread-id" value="4" /><br />
When inside the click
function - the this
element is the element you just clicked. You can use that in order to get the value that you need.
$(function() {
$(document).on('click', '#unread-id', function () {
console.log($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="unread-id" value="1" /><br />
<input id="unread-id" value="2" /><br />
<input id="unread-id" value="3" /><br />
<input id="unread-id" value="4" /><br />
$(function() {
$(document).on('click', '#unread-id', function () {
console.log($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="unread-id" value="1" /><br />
<input id="unread-id" value="2" /><br />
<input id="unread-id" value="3" /><br />
<input id="unread-id" value="4" /><br />
answered Nov 13 '18 at 0:30
Dekel
42.5k54567
42.5k54567
Well,id
should be unique. That is already a bit of an issue.
– Alain Cruz
Nov 13 '18 at 0:31
1
@AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
– Dekel
Nov 13 '18 at 0:33
and btw - your usage if$(this)
after you read my answer is not so nice :)
– Dekel
Nov 13 '18 at 0:36
You know, I only saw the first duplicated id in histd
, until I realized there was another in hisinput
hahaha :-)
– Alain Cruz
Nov 13 '18 at 0:37
1
Well I am upvoting, because you did show it. Still, yours is missing the td'sid
issue, which he/she should fix.
– Alain Cruz
Nov 13 '18 at 0:40
|
show 2 more comments
Well,id
should be unique. That is already a bit of an issue.
– Alain Cruz
Nov 13 '18 at 0:31
1
@AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
– Dekel
Nov 13 '18 at 0:33
and btw - your usage if$(this)
after you read my answer is not so nice :)
– Dekel
Nov 13 '18 at 0:36
You know, I only saw the first duplicated id in histd
, until I realized there was another in hisinput
hahaha :-)
– Alain Cruz
Nov 13 '18 at 0:37
1
Well I am upvoting, because you did show it. Still, yours is missing the td'sid
issue, which he/she should fix.
– Alain Cruz
Nov 13 '18 at 0:40
Well,
id
should be unique. That is already a bit of an issue.– Alain Cruz
Nov 13 '18 at 0:31
Well,
id
should be unique. That is already a bit of an issue.– Alain Cruz
Nov 13 '18 at 0:31
1
1
@AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
– Dekel
Nov 13 '18 at 0:33
@AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
– Dekel
Nov 13 '18 at 0:33
and btw - your usage if
$(this)
after you read my answer is not so nice :)– Dekel
Nov 13 '18 at 0:36
and btw - your usage if
$(this)
after you read my answer is not so nice :)– Dekel
Nov 13 '18 at 0:36
You know, I only saw the first duplicated id in his
td
, until I realized there was another in his input
hahaha :-)– Alain Cruz
Nov 13 '18 at 0:37
You know, I only saw the first duplicated id in his
td
, until I realized there was another in his input
hahaha :-)– Alain Cruz
Nov 13 '18 at 0:37
1
1
Well I am upvoting, because you did show it. Still, yours is missing the td's
id
issue, which he/she should fix.– Alain Cruz
Nov 13 '18 at 0:40
Well I am upvoting, because you did show it. Still, yours is missing the td's
id
issue, which he/she should fix.– Alain Cruz
Nov 13 '18 at 0:40
|
show 2 more comments
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53271142%2fget-variable-inside-certain-div-with-jquery-when-there-are-multiple-divs-with-s%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