How To Get Div Id Jquery
I want to get the ID or class if the link button is clicked
Nick says:
Lorem ipsum dolor sit amet, consectetuer ad
Solution 1:
First of all: You should correct you HTML. Because if your HTML is not loaded correctly in DOM than the Jquery/Javascript also not provide correct result.
In your HTML you are missing some closing tags like - "</p>, </div>
"
<divclass="pane alt"id="a"><h3>Nick says:</h3><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi malesuada</p><p><ahref="#"class="btn-delete">Delete</a><ahref="#"class="btn-unapprove">Unapprove</a><ahref="#"class="btn-approve"style="display:none">Approve</a></p></div>
There are several ways to do accomplish this task.
You Can use Jquery .closest()
$('.btn-unapprove').click(function(){
alert($(this).closest('div.pane.alt').attr('id'));
});
You Can use Jquery .parents()
$('.btn-unapprove').click(function(){
alert($(this).parents('.pane.alt').attr('id'));
});
You Can use Jquery .parent()
$('.btn-unapprove').click(function(){
alert($(this).parent().parent().attr('id'));
});
Solution 2:
This will help you to work done. Before doing this please correct your code and improve like following. In your code you didn't close the <p> tags.
I suggest don't use anchor tags there go with **fake-link** using <span>..
Refer : How to make an anchor tag refer to nothing?
<divclass="pane alt"id="a"><h3>Nick says:</h3><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi malesuada</p><p><ahref="#"class="btn-delete">Delete</a><ahref="#"class="btn-unapprove">Unapprove</a></p></div>
jQuery:
jQuery(document).ready(function(){
jQuery('.btn-unapprove').click(function(){
alert( jQuery(this).closest('div').attr('id') );
});
});
Solution 3:
Use .closest()
in jquery
$('.btn-unapprove').click(function(event){
event.preventDefault();
var id = $(this).closest('div.pane').attr('id');
alert(id);
});
Solution 4:
You have no ids on the buttons elements. If you set it your code will start working.
To get id of container use the code
var id = $(this).closest('.pane').attr('id');
inside the 'click' function.
Post a Comment for "How To Get Div Id Jquery"