JavaScript How to get class name of parent div of html link from which javascript function is called? get class name parent jquery,css class name parent,jquery

How can I get the class name of the parent div from which a html link calls a javascript function?

In the following code example:

<div class="parent-class">
  <a target='_blank' href="javascript:parent(this.parent)">Find parent</a>
</div>

<script>
  function parent(parent) {
    var theValueImLookingFor = (parent.className);
  }
</script>

I would like the "theValueImLookingFor" to be "parent-class", but I keep getting "undefined".

I been trying to figure this out for almost an hour, what can I change to make this work? I understand there are more static ways to get this information, but for my specific purpose I really need to identify the div through the javascript function from which it is called.

Answer:1

You've tagged this with jQuery, which will be the easiest way to find the details of the parent div. Something like

<div class="parent-div">
    <a id="link">Find Parent</a>
</div>

<script>
    $('#link').click(function() {
        alert($(this).parent().attr('class'));
    });
</script>
Answer:2

So I have a service exposed to multiple angular controllers. This service checks if our access token is valid, and if it isn't, makes a request to the server to get a new access token (by sending a ...

So I have a service exposed to multiple angular controllers. This service checks if our access token is valid, and if it isn't, makes a request to the server to get a new access token (by sending a ...

I have a listView of items and these items are to be grouped together based on a class and shown with boxes. Suppose I have 5 items with: <div class="1"></div> <div class="1"></...

I have a listView of items and these items are to be grouped together based on a class and shown with boxes. Suppose I have 5 items with: <div class="1"></div> <div class="1"></...

Trying to figure out the cleanest way to do the following: I'm filtering some results twice, and I'm using Lodash filter to do so. Currently, my code looks like: resultsOne = _.filter(results, ...

Trying to figure out the cleanest way to do the following: I'm filtering some results twice, and I'm using Lodash filter to do so. Currently, my code looks like: resultsOne = _.filter(results, ...

  1. lodash filter with multiple conditions
  2. lodash filter with index
  3. lodash filter with or condition
  4. lodash filter with multiple values
  5. lodash filter with array
  6. lodash filter with regex
  7. lodash filter with keys
  8. lodash filter with map
  9. lodash filter with includes
  10. lodash filter with function
  11. lodash filter with or
  12. lodash filter with limit
  13. lodash filter with object
  14. lodash filter with sort
  15. lodash filter with chain
  16. lodash filter with predicate
  17. lodash filter by value
  18. lodash filter by property
  19. lodash filter by date
  20. lodash filter by multiple properties

I have a problem hiding xAxes and yAxes labels on small screen sizes (mobile phones). I know there is this option: options: { scales: { xAxes: [{ ...

I have a problem hiding xAxes and yAxes labels on small screen sizes (mobile phones). I know there is this option: options: { scales: { xAxes: [{ ...

  1. chartjs hide labels on x axis
  2. chartjs hide labels
  3. chart js hide labels pie
  4. chart js hide labels doughnut
  5. chartjs hide label dataset
  6. chartjs hide label in tooltip
  7. chartjs hide label legend
  8. chartjs hide label color
  9. chartjs hide label x
  10. chartjs hide label line
  11. chartjs hidden label
  12. chartjs hide axis labels
  13. chartjs hide xaxis labels
  14. chartjs hide some labels
  15. chartjs radar hide labels
  16. chartjs hide data labels
  17. chartjs hide tick labels
  18. chartjs pie hide labels
  19. react chartjs hide labels
  20. chartjs hide y axis labels