0

I have the following form and it has one of three divs (which have ids div1, div2 and div3) as a child div:

<form id="myForm" method="GET" action="action.php">
  <div id="div1 or div2 or div3">...</div>
</form>

The div in the form has a button. I need to pass this button's action listener (in JS) the id of the div it is a child of.

So is it possible to get the div which is the parent element of the button (or alternatively the div which is the child element of the form- it's the same div right?) using simple_html_dom parser or something else?

Scimonster
  • 32,893
  • 9
  • 77
  • 89
Solace
  • 8,612
  • 22
  • 95
  • 183
  • You can do: `$(myDiv).parent()` in jQuery. Or `myDiv.parentNode` in just JavaScript. Anyways this question already has an answer here: http://stackoverflow.com/questions/6856871/getting-parent-div-of-element-javascript – Spencer Wieczorek Sep 23 '14 at 14:26
  • Is this about using Simple HTML DOM in PHP, or JavaScript DOM? – Scimonster Sep 23 '14 at 14:27
  • @Scimonster I need to pass the `id` as an argument to the JS function when I am binding this action listener to the button, so obviously I need to get the `id` in PHP. So the question is about PHP! – Solace Sep 23 '14 at 14:31
  • 1
    @Zarah No you don't need to get the *id* in PHP, nor should you. JavaScript should be used for that. This being in a `
    ` means nothing when you are just getting the *id*.
    – Spencer Wieczorek Sep 23 '14 at 14:35

2 Answers2

3

You will need to assign an id to the button, then you can use Javascript to get the parent div:

document.getElementById("buttonId").parentNode;
dseminara
  • 11,665
  • 2
  • 20
  • 22
1

Every element has a parentNode attribute. Access in javascript like so:

var element = document.getElementById('myElementID');
var parent = element.parentNode;
Jared Smith
  • 19,721
  • 5
  • 45
  • 83