0

I get idea from :

Can you check an object's CSS display with JavaScript?

Now look to my code below :

HTML :

<div style="display: none">
 <input type="text" id="myInput" value="5">
</div>

Javascript :

console.log(document.getElementById('myInput').value);
console.log(document.getElementById('myInput').style.display);

Result in my computer is show nothing on my browser console.

My problem :

I need to know the parent of input type text style. cause if the parent style is none make the input type text show nothing when i try to get value of input type text. So the main question is how to get parent style WITHOUT add new id/class/name in div?

2
  • .parentNode? (Possibly recursively.) Or .parent(), with jQuery. You may want window.getComputedStyle() rather than .style, too. Commented Apr 21, 2017 at 6:51
  • Can i get example with your solution for my case ? thank you Commented Apr 21, 2017 at 6:53

3 Answers 3

1

You can use the .parentNode attribute with style in order to get the specific CSS properties you're looking for. All of them are listed on the object. The HTML would be:

  <div style="display: none">
    <div id='one'>
    </div>
  </div>

and the javascript would be:

var node = document.getElementById('one');

console.log(node.parentNode.style.display);

See the fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Is it able to handle if multi level child ?
Yes. You just keep chaining the parentNode attribute.
1

You can use parentElement to get the parent of the input & getComputedStyle to get the style of the parent element

var parentElem = document.getElementById('myInput').parentElement;
//getComputedStyle will give all the properties
console.log(window.getComputedStyle(parentElem, null).display)

DEMO

1 Comment

Is it able to handle if multi level child ?
0

Use .parent() with jQuery here is implemented

$('[type=text]').parent().css({'display':'block', 'background' : 'red','padding':'10px'});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="display: none">
 <input type="text" id="myInput" value="5">
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.