Search code examples
jqueryparent

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.


Solution

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