0

In HTML, I have

 <div>
      <button onclick="action()">button</button>
 </div>

Without giving an ID or class to the div element, what can I do in JavaScript to get an access to it and use it?

Yogesh_D
  • 17,656
  • 10
  • 41
  • 55
  • Use the `closest` selector, or access the element's `parentElement` (put your Javascript in your Javascript section - putting it in HTML attributes is as bad as `eval`) – CertainPerformance May 02 '18 at 06:20

4 Answers4

3

Pass this into action:

<button onclick="action(this)">button</button>

and then in action

function action(btn) {
    var div = btn.parentNode;
    // ...
}

or if you want a bit more flexibility, use the (relatively-new) closest method:

function action(btn) {
    var div = btn.closest("div");
    // ...
}

Side note: Rather than onxyz-attribute-style event handlers, consider using modern event handling (addEventListener, attachEvent on obsolete browsers). If you have to support obsolete browsers, my answer here provides a function you can use to deal with the lack of addEventListener.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Doesn't "this" refer to the button? –  May 02 '18 at 06:25
  • @ronnLee: Not if you use `onclick="action()"`, no. It does if you use modern event handling. – T.J. Crowder May 02 '18 at 06:26
  • So, other than using it on "onclick="action()", this usually refers to the current element? –  May 02 '18 at 06:26
  • @ronnLee: Sorry, I misunderstood what you were asking in your comment and was unclear. In `onclick="action(this)"`, yes, `this` refers to the button (the element that attribute is on). But *within* `action`, `this` would not refer to the button (that's why I have you pass it in). With modern event handling, `this` within `action` would refer to the button. – T.J. Crowder May 02 '18 at 06:40
0

using this in the onclick=action() and then the parent is .parentNode then your method would look like :

<div>
  <button onclick="action(this);">button</button>
</div>

function action(el) {
  console.log(el.parentNode);
}

but I rather prefer .addEventListenerand for your next question then :

var $el = document.getElementById("a");
$el.addEventListener("click", function(e) {
  // Create a <button> element
  var btn = document.createElement("BUTTON");
  // Create a text node 
  var t = document.createTextNode("CLICK ME");
  // Append the text to <button>
  btn.appendChild(t);        
  // Append <button> to the parentNode          
  this.parentNode.appendChild(btn);
});
<div>
  <button id="a">Button</button>
</div>

Also in case you wonder for a shorter version and probably introducing to the jQuery :

;$(function(){
  $("#a").on("click", function(e) {  
    $(this).parent().append('<button>Click Me</button>');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <button id="a">Button</button>
</div>

Another but not straight way to get the parent instead of .parentNode is .closest() method:

element.closest("div"); // also supported in jQuery

Returns the first ancestor of element, that is a <div> element:

nullqube
  • 2,959
  • 19
  • 18
  • What if i want to add another button after clicking it, do i just say "function action(e){ e.parentNode.appendChild(button);} –  May 02 '18 at 06:31
  • @ronnLee & nullqube: `e` is a very poor choice of name for that parameter, since `action` is an event handler and in most cases, an event handler accepting a parameter called `e` is receiving an Event object -- but the `action` above is **not**. *"if I want to add another button"* Yes, that would work. Just change the name of the parameter. :-) – T.J. Crowder May 02 '18 at 06:43
0

Grab the event then get the currentTarget then find the parentNode. Very simple way to do it. This will work no matter which element is clicked. Please see code snippet demonstration.

function getParentNode(event) {
  console.log(event.currentTarget.parentNode);
}
<div id="div1"><button onclick="getParentNode(event)">Click</button></div>
<div id="div2"><button onclick="getParentNode(event)">Click</button></div>
Cory Kleiser
  • 1,969
  • 2
  • 13
  • 26
-1

If it is possible, pass this (button) to action. Then you can access parent by element.parentNode. Example: https://jsbin.com/fidufa/edit

Branislav
  • 111
  • 1
  • 3