how to get a parent element from a child with id?

2

I have the following code:

<div></div>

<div>
  <div class='minhaClasse'>
    <div id='text'>hhaushdus</div>
    <input name='profile_id' />
  <div>
</div>

I need to get this div :

<div class='minhaClasse'>
  <div id='text'>hhaushdus</div>
  <input name='profile_id' />
<div>

I tried this way:

let element = document.getElementsByName('profile_id')[0].parentNode;

This returned my father:

<div>
  <div class='minhaClasse'>
    <div id='text'>hhaushdus</div>
    <input name='profile_id' />
  <div>
</div>

Basically, these are two questions, how do I get this div with the class 'myClass' and if you have a getElement of a getElement type:

document.getElementsByName('profile_id')[0].parentNode.getElementByClassName('minhaClasse'));
    
asked by anonymous 11.10.2018 / 22:37

2 answers

1

You can do the following:

x = document.getElementsByClassName("minhaClasse");

This will return all elements that have this class, then just you iterate and perform the process you want:

for (var i = 0; i < x.length; i++) {
    //seu código aqui
}
    
11.10.2018 / 22:45
0

I recommend taking a look at link I can not remember the correct syntax but I think this specific code can help you link

EDIT

Example:

function myFunction() {
    var c = document.getElementById("myDIV").childNodes;
    c[1].style.backgroundColor = "yellow";
}
<div></div>

<div id="myDIV">PAI
  <div class='minhaClasse'>FILHO
    <div id='text'>hhaushdus</div>
    <input name='profile_id' />
  <div>
</div>

<button onclick="myFunction()">Try it</button>
    
11.10.2018 / 22:41