JavaScript Get id value of nearest class

I have a div table and would like to get the id value of the class live-mu-table-tdq that is to the left of where I click. I thought that the following code would work but it didn't.

 $(this).closest(".live-mu-table-tdq").attr("id");

fiddle

<div class="live-mu-table" >
    <div class="live-mu-tablee-tr">
        <div class="live-mu-table-tdq" id="q-1">q1 goes here</div>
        <div class="live-mu-table-tdspacer1"></div>
        <div class="live-mu-table-tda" id="a-3"> 3 the answer</div>
    </div>

    <div class="live-mu-tablee-tr">
        <div class="live-mu-table-tdq" id="q-2">q2 question</div>
        <div class="live-mu-table-tdspacer1"></div>
        <div class="live-mu-table-tda" id="a-1">answer 1</div>
    </div>

    <div class="live-mu-tablee-tr">
        <div class="live-mu-table-tdq" id="q-3">question 3</div>
        <div class="live-mu-table-tdspacer1"></div>
        <div class="live-mu-table-tda" id="a-2">the answer 2</div>
    </div>
</div>
$(".live-mu-table-tda").click(function() { 
    var abc = $(this).closest(".live-mu-table-tdq").attr("id");
    alert (abc)   
});
Answer:1

The .live-mu-table-tdq element is a sibling of the live-mu-table-tda element so you need to use siblings(), not closest():

$(".live-mu-table-tda").click(function() {
    var abc = $(this).siblings(".live-mu-table-tdq").prop("id");
    alert(abc)
});

Updated fiddle

Answer:2

.closest() look upwards in the DOM. I believe .siblings(".live-mu-table-tdq").attr("id") is what you are after

https://api.jquery.com/siblings/

Answer:3

Is it possible to create dynamic templates/pages with TVML without relying on Apple's standard templates? Say I'd like to create a template that has 2 images next to each other and on top of these ...

Is it possible to create dynamic templates/pages with TVML without relying on Apple's standard templates? Say I'd like to create a template that has 2 images next to each other and on top of these ...

My visual editor in WordPress doesn't work and I figured out that was because my computer doesn't load the full tinymce folder. It only loaded "mce-view.js". In another computer, using Chrome, around ...

My visual editor in WordPress doesn't work and I figured out that was because my computer doesn't load the full tinymce folder. It only loaded "mce-view.js". In another computer, using Chrome, around ...

  1. chrome fails to load pages
  2. chrome fails to load
  3. chrome failed to load pdf
  4. chrome fails to load any page
  5. google chrome fails to load pages
  6. google chrome fails to load
  7. google chrome failed to load web pages
  8. chrome randomly fails to load pages

I am unable to append the page just seems to refresh where as i want to view it without refreshing the page index.php <div class="view_comment"> <b>username :</b> <?php ...

I am unable to append the page just seems to refresh where as i want to view it without refreshing the page index.php <div class="view_comment"> <b>username :</b> <?php ...

So I'm working on a WPF app that has a web page self hosting, and in my Javascript I need to use some data from my C#. I'm wondering if there's any way (in WPF/C#) to invoke a Javascript function ...

So I'm working on a WPF app that has a web page self hosting, and in my Javascript I need to use some data from my C#. I'm wondering if there's any way (in WPF/C#) to invoke a Javascript function ...