Send AJAX request by clicking a link inside a dynamically created bootstrap accordion












0















I have a dynamically created accordion like this:



<div class="panel-body">
<div class="panel-group" id="accordion">

<div class="panel panel-default" >
<div class="panel-heading">
<h5 class="panel-title small">
<a data-toggle="collapse" data-parent="#accordion" href="#1" class="tasktitle" data-id="1" id="opentask">$name</a>
</h5>
</div>
<div id="1" class="panel-collapse collapse">
<div class="panel-body">body text</div>
</div>

<div class="panel panel-default" >
<div class="panel-heading">
<h5 class="panel-title small">
<a data-toggle="collapse" data-parent="#accordion" href="#2" class="tasktitle" data-id="2" id="opentask">$name</a>
</h5>
</div>
<div id="2" class="panel-collapse collapse">
<div class="panel-body">body text</div>
</div>

</div>
</div>


Then I have a static value like $loggeduser which is always the same for the logged user.



As this is a dynamically created accordion though, the id of each accordion section is different



What I am trying to achieve is when the user click to open the accordion, in the same time to execute an AJAX request which will send the value of the $loggeduser together with the id of the accordion in which the clicked link is placed in.



This is the ajax I am using to send the info to the file



$(function(){    
$("#opentask").click(function(){
$.get("markread-task.php?id=ID&user=LOGGEDUSERCOMMENT",function(data){
});
});
return false;
});


but I cant figure out how to to have a different ID in this link



markread-task.php?id=ID&user=LOGGEDUSERCOMMENT


for example if the user click on the link that opend accordion 2, the ajax sends link like



markread-task.php?id=2&user=LOGGEDUSERCOMMENT


Thanks for your help










share|improve this question























  • We would need more code in order to help you here, there could be many possible solutions depending on how it's implemented.

    – Armel
    Jan 3 at 13:44











  • what else do you need ?

    – lStoilov
    Jan 3 at 14:40
















0















I have a dynamically created accordion like this:



<div class="panel-body">
<div class="panel-group" id="accordion">

<div class="panel panel-default" >
<div class="panel-heading">
<h5 class="panel-title small">
<a data-toggle="collapse" data-parent="#accordion" href="#1" class="tasktitle" data-id="1" id="opentask">$name</a>
</h5>
</div>
<div id="1" class="panel-collapse collapse">
<div class="panel-body">body text</div>
</div>

<div class="panel panel-default" >
<div class="panel-heading">
<h5 class="panel-title small">
<a data-toggle="collapse" data-parent="#accordion" href="#2" class="tasktitle" data-id="2" id="opentask">$name</a>
</h5>
</div>
<div id="2" class="panel-collapse collapse">
<div class="panel-body">body text</div>
</div>

</div>
</div>


Then I have a static value like $loggeduser which is always the same for the logged user.



As this is a dynamically created accordion though, the id of each accordion section is different



What I am trying to achieve is when the user click to open the accordion, in the same time to execute an AJAX request which will send the value of the $loggeduser together with the id of the accordion in which the clicked link is placed in.



This is the ajax I am using to send the info to the file



$(function(){    
$("#opentask").click(function(){
$.get("markread-task.php?id=ID&user=LOGGEDUSERCOMMENT",function(data){
});
});
return false;
});


but I cant figure out how to to have a different ID in this link



markread-task.php?id=ID&user=LOGGEDUSERCOMMENT


for example if the user click on the link that opend accordion 2, the ajax sends link like



markread-task.php?id=2&user=LOGGEDUSERCOMMENT


Thanks for your help










share|improve this question























  • We would need more code in order to help you here, there could be many possible solutions depending on how it's implemented.

    – Armel
    Jan 3 at 13:44











  • what else do you need ?

    – lStoilov
    Jan 3 at 14:40














0












0








0








I have a dynamically created accordion like this:



<div class="panel-body">
<div class="panel-group" id="accordion">

<div class="panel panel-default" >
<div class="panel-heading">
<h5 class="panel-title small">
<a data-toggle="collapse" data-parent="#accordion" href="#1" class="tasktitle" data-id="1" id="opentask">$name</a>
</h5>
</div>
<div id="1" class="panel-collapse collapse">
<div class="panel-body">body text</div>
</div>

<div class="panel panel-default" >
<div class="panel-heading">
<h5 class="panel-title small">
<a data-toggle="collapse" data-parent="#accordion" href="#2" class="tasktitle" data-id="2" id="opentask">$name</a>
</h5>
</div>
<div id="2" class="panel-collapse collapse">
<div class="panel-body">body text</div>
</div>

</div>
</div>


Then I have a static value like $loggeduser which is always the same for the logged user.



As this is a dynamically created accordion though, the id of each accordion section is different



What I am trying to achieve is when the user click to open the accordion, in the same time to execute an AJAX request which will send the value of the $loggeduser together with the id of the accordion in which the clicked link is placed in.



This is the ajax I am using to send the info to the file



$(function(){    
$("#opentask").click(function(){
$.get("markread-task.php?id=ID&user=LOGGEDUSERCOMMENT",function(data){
});
});
return false;
});


but I cant figure out how to to have a different ID in this link



markread-task.php?id=ID&user=LOGGEDUSERCOMMENT


for example if the user click on the link that opend accordion 2, the ajax sends link like



markread-task.php?id=2&user=LOGGEDUSERCOMMENT


Thanks for your help










share|improve this question














I have a dynamically created accordion like this:



<div class="panel-body">
<div class="panel-group" id="accordion">

<div class="panel panel-default" >
<div class="panel-heading">
<h5 class="panel-title small">
<a data-toggle="collapse" data-parent="#accordion" href="#1" class="tasktitle" data-id="1" id="opentask">$name</a>
</h5>
</div>
<div id="1" class="panel-collapse collapse">
<div class="panel-body">body text</div>
</div>

<div class="panel panel-default" >
<div class="panel-heading">
<h5 class="panel-title small">
<a data-toggle="collapse" data-parent="#accordion" href="#2" class="tasktitle" data-id="2" id="opentask">$name</a>
</h5>
</div>
<div id="2" class="panel-collapse collapse">
<div class="panel-body">body text</div>
</div>

</div>
</div>


Then I have a static value like $loggeduser which is always the same for the logged user.



As this is a dynamically created accordion though, the id of each accordion section is different



What I am trying to achieve is when the user click to open the accordion, in the same time to execute an AJAX request which will send the value of the $loggeduser together with the id of the accordion in which the clicked link is placed in.



This is the ajax I am using to send the info to the file



$(function(){    
$("#opentask").click(function(){
$.get("markread-task.php?id=ID&user=LOGGEDUSERCOMMENT",function(data){
});
});
return false;
});


but I cant figure out how to to have a different ID in this link



markread-task.php?id=ID&user=LOGGEDUSERCOMMENT


for example if the user click on the link that opend accordion 2, the ajax sends link like



markread-task.php?id=2&user=LOGGEDUSERCOMMENT


Thanks for your help







javascript jquery ajax






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 13:40









lStoilovlStoilov

2551418




2551418













  • We would need more code in order to help you here, there could be many possible solutions depending on how it's implemented.

    – Armel
    Jan 3 at 13:44











  • what else do you need ?

    – lStoilov
    Jan 3 at 14:40



















  • We would need more code in order to help you here, there could be many possible solutions depending on how it's implemented.

    – Armel
    Jan 3 at 13:44











  • what else do you need ?

    – lStoilov
    Jan 3 at 14:40

















We would need more code in order to help you here, there could be many possible solutions depending on how it's implemented.

– Armel
Jan 3 at 13:44





We would need more code in order to help you here, there could be many possible solutions depending on how it's implemented.

– Armel
Jan 3 at 13:44













what else do you need ?

– lStoilov
Jan 3 at 14:40





what else do you need ?

– lStoilov
Jan 3 at 14:40












1 Answer
1






active

oldest

votes


















2














First of all, Id must be unique to each element. Use class or name instead. So you cannot have two elements with id="opentask".



To get data-id value in your click method:



$(function(){    
$(".opentask").click(function(){
var id = $(this).data('id');
$.get("markread-task.php?id="+id+"&user=LOGGEDUSERCOMMENT",function(data){
});
});
return false;
});





share|improve this answer





















  • 1





    I would suggest that a pseudo class be used to get round the unique id. e.g. $(".tasktitle").click(function(){ ......

    – Steve
    Jan 3 at 14:04













  • @maximelian1986, I have tried that,but it doesn't work

    – lStoilov
    Jan 3 at 14:51











  • @Steve, I have tried that too, but still doesn't work

    – lStoilov
    Jan 3 at 14:52











  • @maximelian1986, It worked actually. I was passing the wrong id so, didn't work at that time.

    – lStoilov
    Jan 3 at 20:03












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%2f54023453%2fsend-ajax-request-by-clicking-a-link-inside-a-dynamically-created-bootstrap-acco%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









2














First of all, Id must be unique to each element. Use class or name instead. So you cannot have two elements with id="opentask".



To get data-id value in your click method:



$(function(){    
$(".opentask").click(function(){
var id = $(this).data('id');
$.get("markread-task.php?id="+id+"&user=LOGGEDUSERCOMMENT",function(data){
});
});
return false;
});





share|improve this answer





















  • 1





    I would suggest that a pseudo class be used to get round the unique id. e.g. $(".tasktitle").click(function(){ ......

    – Steve
    Jan 3 at 14:04













  • @maximelian1986, I have tried that,but it doesn't work

    – lStoilov
    Jan 3 at 14:51











  • @Steve, I have tried that too, but still doesn't work

    – lStoilov
    Jan 3 at 14:52











  • @maximelian1986, It worked actually. I was passing the wrong id so, didn't work at that time.

    – lStoilov
    Jan 3 at 20:03
















2














First of all, Id must be unique to each element. Use class or name instead. So you cannot have two elements with id="opentask".



To get data-id value in your click method:



$(function(){    
$(".opentask").click(function(){
var id = $(this).data('id');
$.get("markread-task.php?id="+id+"&user=LOGGEDUSERCOMMENT",function(data){
});
});
return false;
});





share|improve this answer





















  • 1





    I would suggest that a pseudo class be used to get round the unique id. e.g. $(".tasktitle").click(function(){ ......

    – Steve
    Jan 3 at 14:04













  • @maximelian1986, I have tried that,but it doesn't work

    – lStoilov
    Jan 3 at 14:51











  • @Steve, I have tried that too, but still doesn't work

    – lStoilov
    Jan 3 at 14:52











  • @maximelian1986, It worked actually. I was passing the wrong id so, didn't work at that time.

    – lStoilov
    Jan 3 at 20:03














2












2








2







First of all, Id must be unique to each element. Use class or name instead. So you cannot have two elements with id="opentask".



To get data-id value in your click method:



$(function(){    
$(".opentask").click(function(){
var id = $(this).data('id');
$.get("markread-task.php?id="+id+"&user=LOGGEDUSERCOMMENT",function(data){
});
});
return false;
});





share|improve this answer















First of all, Id must be unique to each element. Use class or name instead. So you cannot have two elements with id="opentask".



To get data-id value in your click method:



$(function(){    
$(".opentask").click(function(){
var id = $(this).data('id');
$.get("markread-task.php?id="+id+"&user=LOGGEDUSERCOMMENT",function(data){
});
});
return false;
});






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 14:06

























answered Jan 3 at 13:49









maximelian1986maximelian1986

907519




907519








  • 1





    I would suggest that a pseudo class be used to get round the unique id. e.g. $(".tasktitle").click(function(){ ......

    – Steve
    Jan 3 at 14:04













  • @maximelian1986, I have tried that,but it doesn't work

    – lStoilov
    Jan 3 at 14:51











  • @Steve, I have tried that too, but still doesn't work

    – lStoilov
    Jan 3 at 14:52











  • @maximelian1986, It worked actually. I was passing the wrong id so, didn't work at that time.

    – lStoilov
    Jan 3 at 20:03














  • 1





    I would suggest that a pseudo class be used to get round the unique id. e.g. $(".tasktitle").click(function(){ ......

    – Steve
    Jan 3 at 14:04













  • @maximelian1986, I have tried that,but it doesn't work

    – lStoilov
    Jan 3 at 14:51











  • @Steve, I have tried that too, but still doesn't work

    – lStoilov
    Jan 3 at 14:52











  • @maximelian1986, It worked actually. I was passing the wrong id so, didn't work at that time.

    – lStoilov
    Jan 3 at 20:03








1




1





I would suggest that a pseudo class be used to get round the unique id. e.g. $(".tasktitle").click(function(){ ......

– Steve
Jan 3 at 14:04







I would suggest that a pseudo class be used to get round the unique id. e.g. $(".tasktitle").click(function(){ ......

– Steve
Jan 3 at 14:04















@maximelian1986, I have tried that,but it doesn't work

– lStoilov
Jan 3 at 14:51





@maximelian1986, I have tried that,but it doesn't work

– lStoilov
Jan 3 at 14:51













@Steve, I have tried that too, but still doesn't work

– lStoilov
Jan 3 at 14:52





@Steve, I have tried that too, but still doesn't work

– lStoilov
Jan 3 at 14:52













@maximelian1986, It worked actually. I was passing the wrong id so, didn't work at that time.

– lStoilov
Jan 3 at 20:03





@maximelian1986, It worked actually. I was passing the wrong id so, didn't work at that time.

– lStoilov
Jan 3 at 20:03




















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%2f54023453%2fsend-ajax-request-by-clicking-a-link-inside-a-dynamically-created-bootstrap-acco%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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas