How to get parent value

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.

Answers


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);
})

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');

Need Your Help

Custom input range on vertical Orientation issue

html css css3 orientation input-type-range

I have tried to create Customized Slider with Vertical Orientation using the following ways,

selecting all fields prepared statements

php sql class prepared-statement

Alright so I'm converting all my sql statements to prepared statements and I cannot seem to figure out why this doesn't return any data: