简体   繁体   中英

How to get parentnode of select element in javascript

I am trying to alter the background color of a table cell if the selected item in a dropdown is changed. I use the same javascript for a textbox and it works fine. In firebug, "cell" is undefined when called from the select.

Here is my script/html

<html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
  <title>Untitled Page</title>
   <script type="text/javascript">
    function changeText(cell, shown, hidden)
    {
     if (shown == hidden)
     {
      cell.style.backgroundColor="red";
     }
     else
     {
      cell.style.backgroundColor="green";
     }
    }
   </script>
  </head>
  <body>
   <form id="form1" runat="server">
    <table cellpadding="5">
     <tr>
      <td>
       Cell 1
      </td>
     <td>
      <select id="catBlah" OnChange="changeText(this.parentnode, this.options[this.selectedIndex].value, '789');">
       <option value=""></option>
       <option selected="selected" value="789">Item 1</option>
       <option value="000">Item 2</option>
       <option value="456">Item 3</option>
       <option value="123">Item 4</option>
      </select>
     </td>
    </tr>
    <tr>
     <td>
      <input type="text" value="blue" onchange="changeText(this.parentNode, this.value, 'blue');" />
     </td>
     <td>
      Cell 4
     </td>
    </tr>
   </table>
  </form>
 </body>
</html>

If I just pass the select object (using "this" instead of "this.parentnode"), I can change the select's background color (which might meet requirements), but I can't figure out how to get the parentnode.

Thanks

It's parentNode (note the casing)

I believe that your code was working (I may have fixed it by fiddling with it). It looks like it was your casing. Please see: http://jsfiddle.net/kaleb/y6UuL/

It appears that you were just not able to see the background color. I added padding to your cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM