Anyone know how can I get the id of
This is my html tag:
<div id="grade_contamina">
<ul id="ul_element">
<!---My first li--->
<li id="first_id">
<p>
Apple
</p>
<!---Button for apple---->
<span class="fruits" id="btn_apple">
<a class="btn_send_once">Send to Students</a>
<a class="btn_delete_once" >Delete</a>
<a class="btn_download_once">Download</a>
</span>
</li>
<!---My second li--->
<li id="second_id">
<p>
Grapes
</p>
<!---Button for grapes---->
<span class="fruits" id="btn_grapes">
<a class="btn_send_once">Send to Students</a>
<a class="btn_delete_once" >Delete</a>
<a class="btn_download_once">Download</a>
</span>
</li>
</ul>
</div>
Every LI element there is 3 button, the delete, send, and download. let say from the 'apple LI element', i clicked the delete. i need to delete this LI element base on its id, or i want to send this base on its id. now how can i get the id of LI or what is the best way to get the ID of LI element?
aside from getting the id, i want to fire a JQUERY effect when the button delete is clicked.
I want this happen if the button delete is clicked.
**Remove the LI element which is clicked
**Apped a new LI element at the last LI element of ul using "FadeIn Effect".
this is my pseudo code
if button is clicked{
var the_id = get the id of LI where the button is clicked
now which one of button is clicked?
if button send is clicked{
some code here, i will need here the id to send...
}else if button download is clicked{
some code here, i will need here the id too..
}else if button delete is clicked{
remove the LI element (With fadeOut effect if possible)
append a new LI element at the end of LI whith fadeIn effect
}
//End of Statement
}
Anyone can help me? I only have small knowledge in jQuery, so any help will be appreciated. Thank You!!!
Anyone know how can I get the id of
This is my html tag:
<div id="grade_contamina">
<ul id="ul_element">
<!---My first li--->
<li id="first_id">
<p>
Apple
</p>
<!---Button for apple---->
<span class="fruits" id="btn_apple">
<a class="btn_send_once">Send to Students</a>
<a class="btn_delete_once" >Delete</a>
<a class="btn_download_once">Download</a>
</span>
</li>
<!---My second li--->
<li id="second_id">
<p>
Grapes
</p>
<!---Button for grapes---->
<span class="fruits" id="btn_grapes">
<a class="btn_send_once">Send to Students</a>
<a class="btn_delete_once" >Delete</a>
<a class="btn_download_once">Download</a>
</span>
</li>
</ul>
</div>
Every LI element there is 3 button, the delete, send, and download. let say from the 'apple LI element', i clicked the delete. i need to delete this LI element base on its id, or i want to send this base on its id. now how can i get the id of LI or what is the best way to get the ID of LI element?
aside from getting the id, i want to fire a JQUERY effect when the button delete is clicked.
I want this happen if the button delete is clicked.
**Remove the LI element which is clicked
**Apped a new LI element at the last LI element of ul using "FadeIn Effect".
this is my pseudo code
if button is clicked{
var the_id = get the id of LI where the button is clicked
now which one of button is clicked?
if button send is clicked{
some code here, i will need here the id to send...
}else if button download is clicked{
some code here, i will need here the id too..
}else if button delete is clicked{
remove the LI element (With fadeOut effect if possible)
append a new LI element at the end of LI whith fadeIn effect
}
//End of Statement
}
Anyone can help me? I only have small knowledge in jQuery, so any help will be appreciated. Thank You!!!
Share Improve this question edited May 20, 2015 at 6:39 Amal Dev 1,9631 gold badge14 silver badges26 bronze badges asked May 20, 2015 at 5:44 bernzkiebernzkie 1,2693 gold badges18 silver badges35 bronze badges 2- hi, what do you mean by li base? – Sourabh Kumar Sharma Commented May 20, 2015 at 5:46
- 1 this.id should do the trick. – mwilson Commented May 20, 2015 at 5:49
5 Answers
Reset to default 4To get the id
of li
on the a
click, you need to use closest
.
This will get the closest li
ancestor of the clicked element.
$('#grade_contamina').on('click', 'a', function() {
alert($(this).closest('li').attr('id'));
return false;
});
<div id="grade_contamina">
<ul id="ul_element">
<!---My first li--->
<li id="first_id">
<p>
Apple
</p>
<!---Button for apple---->
<span class="fruits" id="btn_apple">
<a class="btn_send_once">Send to Students</a>
<a class="btn_delete_once" >Delete</a>
<a class="btn_download_once">Download</a>
</span>
</li>
<!---My second li--->
<li id="second_id">
<p>
Grapes
</p>
<!---Button for grapes---->
<span class="fruits" id="btn_grapes">
<a class="btn_send_once">Send to Students</a>
<a class="btn_delete_once" >Delete</a>
<a class="btn_download_once">Download</a>
</span>
</li>
</ul>
</div>
$(document).ready(function(){
$('#grade_contamina').on('click', 'a', function() {
var btn_name = $(this).attr('class')
var id = $(this).closest('li').attr('id');
if(btn_name == 'btn_send_once'){
alert(btn_name+" within "+id);
//your code
}else if(btn_name == 'btn_delete_once'){
alert(btn_name+" within "+id);
}else{
alert(btn_name+" within "+id);
}
})
})
The above code will get you the button
's class
along with its li
's id
You could use jQuerys .attr(propertyname) like I did in this fiddle: https://jsfiddle/d907e6gm/
$('ul > li').click(function(){
alert($(this).attr('id'));
});
I am not going to give all the code. I will just show you, how to get list item id and which button you have clicked.
$(document).ready(function(){
$("#ul_element a").on("click",function(){
idli = $(this).closest("li").attr("id"); // your list item id.
val = $(this).text(); // your link text
});
});
DEMO
Try filtering .parents()
of clicked a
, returning id
of a
parent li
$("#grade_contamina a").on("click", function() {
console.log($(this).parents("li")[0].id)
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="grade_contamina">
<ul id="ul_element">
<!---My first li--->
<li id="first_id">
<p>
Apple
</p>
<!---Button for apple---->
<span class="fruits" id="btn_apple">
<a class="btn_send_once">Send to Students</a>
<a class="btn_delete_once" >Delete</a>
<a class="btn_download_once">Download</a>
</span>
</li>
<!---My second li--->
<li id="second_id">
<p>
Grapes
</p>
<!---Button for grapes---->
<span class="fruits" id="btn_grapes">
<a class="btn_send_once">Send to Students</a>
<a class="btn_delete_once" >Delete</a>
<a class="btn_download_once">Download</a>
</span>
</li>
</ul>
</div>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742328367a4423208.html
评论列表(0条)