Simple javascript in page not working
-
I’m trying to implement a simple javascript inside a particular page of my WordPress site. The code works perfectly in a standalone html file, but once I put the exact same code in the “text” section of my “edit page”, update and refresh my page, the script does not execute. Here is the code :
<script type="text/javascript"> function dropdownSelected() { document.getElementById("myTable").innerHTML=""; // Some table manipulations depending on "mySelect" will happen here. var newTable = new String("<h2>Fine.</h2>"); document.getElementById("myTable").innerHTML=newTable; } </script> <select id="mySelect" onchange="dropdownSelected()"> <option value="" disabled selected style="display:none;">Pick option</option> <option value="1">test1</option> <option value="2">test2</option> <option value="3">test3</option> </select> <table id="myTable" border="1"> <tr> <th>Header 1</th> <th>Header 2</th> </tr> <tr> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td>row 2, cell 1</td> <td>row 2, cell 2</td> </tr> <tr> <td>row 3, cell 1</td> <td>row 3, cell 2</td> </tr> </table>
It can’t get any simpler, yet it doesn’t work in WP. What am I doing wrong? I noticed that removing the <h2></h2> tags in my newTable string lets the script execute. As far as I know, I should be able to put whatever I want in this string though!
I simply want to adjust the content of an html table according the the user’s choice in a dropdown list. The easiest should be to work on a custom string containing the table’s html code and then put it in the table’s innerHTML… I just wish it worked in WP as it does in a normal html file.
- The topic ‘Simple javascript in page not working’ is closed to new replies.