Jump to content

Recommended Posts

Trying to create a simple document that gets the parentNode then applies a background color. My getElementByTagName is "p" so the parentNode would be the <body>. The background color should then be applied to the entire document, right?  I'm getting an "object expected" error.

 

http://goken68.brinkster.net/ParentNode.html

 


//purpose is to use parent node and change the background color
<html>
<body>
<head>
<script type="text/javascript" language="javascript">

function changeIt() {
var node;
node = document.getElementsByTagName("p").parentNode;
node.style.background-color = '#0033dd';
}


</script>

</head>

<div id="teams">
<h1>NFL Teams</h1>

<h2>NFC North</h2>
<p>Chicago Bears</p>
<p>Green Bay Packers</p>
<p>Minnesota Vikings</p>
<p>Detroit Lions</p>

<h2>NFC South</h2>
<p>New Orleans Saints</p>
<p>Atlanta Falcons</p>
<p>Carolina Panthers</p>
<p>Tampa Bay Buccannears</p>

<h2>NFC East</h2>
<p>Dallas Cowboys</p>
<p>Washington Redskins</p>
<p>Philadelphia Eagles</p>
<p>NY Giants</p>

<h2>NFC West</h2>
<p>San Francisco 49ers</p>
<p>Arizona Cardinals</p>
<p>Seattle Seahawks</p>
<p>St.Louis Rams</p>
</div>

<input type="button" value="change background color" onClick="changeIt()">



</body>
</html>


 

 

Link to comment
https://forums.phpfreaks.com/topic/256620-how-to-get-the-parentnode/
Share on other sites

Background colour is a problem. It is denoted in javascript as backgroundColor. In fact, I think all javascript notations of a css style use camels as opposed to the dash.

 

Also, you will need to pick a position of the getElementsByTagName array for the javascript to get the parent of it. parentNode works by getting the parent of a specific element. Not an array of elements.

 

Try:

function changeIt() {
var node;
node = document.getElementsByTagName("p")[0].parentNode;
node.style.backgroundColor = '#0033dd';
}

 

Hope that helps you,

Joe

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.