Reputation: 1
I have this basic code in my html document:
<div class="database_name" style="background: rgb(243, 243, 243);">
<i class="minus_sign fa fa-minus-circle"></i>
<div id="hovering_div_inside">
<i class="fa fa-database database_sign"></i>
<span class="dat_name">Example</span>
</div>
<div class="appended">
<div class="create_new_table">
<i class="fa fa-pencil">
<span class="create_new_table_span">New</span>
</i>
</div>
<span class="table_names_from_database"></span>
</div>
</div>
I need to get the word example $(".dat_name).text()
when ever I click on $(".create_new_table_span")
on New.
It should be dynamic not only one time and also there are many NEW
links selector parent()
does not work.
Upvotes: 0
Views: 55
Reputation: 1745
You can do something like this:
$(".create_new_table_span").click(getDataName);
function getDataName(e){
var name = $(e.target).closest('.database_name').find('.dat_name').text();
//do what ever you want with the name.
}
Using the closest() Method you will get the first nearest element, matching the selector. And find() searches the children tree of an element.
You can also use find() directly on the click target:
$(e.target).find('.dat_name');
Upvotes: 0
Reputation: 20750
Find the closest .database_name
and then .dat_name
under it on click of .create_new_table_span
like following.
$('.create_new_table_span').click(function(){
var text = $(this).closest('.database_name').find('.dat_name').text();
alert(text);
})
Upvotes: 1