Title: Simple Java Script not working. What am i doing wrong?
Last modified: September 13, 2016

---

# Simple Java Script not working. What am i doing wrong?

 *  [tanckom](https://wordpress.org/support/users/tanckom/)
 * (@tanckom)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/simple-java-script-not-working-what-am-i-doing-wrong/)
 * So, I got this Javascript, which I successfully tested on w3schools.com’s editor
 *     ```
       Name: <input type="text" id="myText" value="Mickey">
   
       <p id='test'>Click the button to change the value of the text field.</p>
   
       <button onclick="myFunction()">Try it</button>
   
       <script>
       function myFunction() {
       var text = document.getElementById('myText').value;
       text += ' blah';
       document.getElementById("myText").value = text;
       }
       </script>
       ```
   
 * However when using it on WordPress Text Editor (the one where you can edit HTML)
   and going back to Visual, there’s nothing changing in the textbox with id myText.
 * After looking it up, i saw, that WordPress removed the onclick=”myFunction() 
   part in <button..
 * Why, and how can I fix this or use this code?

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [9 years, 8 months ago](https://wordpress.org/support/topic/simple-java-script-not-working-what-am-i-doing-wrong/#post-8177570)
 * The Visual editor is not meant to be used to preview the Text editor and doing
   so will strip your code. You need to use the ‘Preview’ button to preview your
   changes.
 * If it still doesn’t work then let us know.
 *  Thread Starter [tanckom](https://wordpress.org/support/users/tanckom/)
 * (@tanckom)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/simple-java-script-not-working-what-am-i-doing-wrong/#post-8177588)
 * No, still not working.
    As you can see here: [https://xs-sol.com/testp/](https://xs-sol.com/testp/)
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [9 years, 8 months ago](https://wordpress.org/support/topic/simple-java-script-not-working-what-am-i-doing-wrong/#post-8177667)
 * Looks like there’s still some stripping going on.
    Try installing this plugin:
   [https://wordpress.org/plugins/dont-muck-my-markup/](https://wordpress.org/plugins/dont-muck-my-markup/)
 * Alternatively, try to turn off the rich editor (from the dashboard, go to Users
   > Your Profile > Personal Options
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [9 years, 8 months ago](https://wordpress.org/support/topic/simple-java-script-not-working-what-am-i-doing-wrong/#post-8177669)
 * Then try re-adding the JavaScript code
 *  Thread Starter [tanckom](https://wordpress.org/support/users/tanckom/)
 * (@tanckom)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/simple-java-script-not-working-what-am-i-doing-wrong/#post-8177892)
 * This plugin didn’t work:
    [https://wordpress.org/plugins/dont-muck-my-markup/](https://wordpress.org/plugins/dont-muck-my-markup/)
 * However by disabling Visual Editor, and simply Updating the post with Text Editor,
   did the thing. So there’s something with Visual that removes it or makes it non
   working.
    As you can see, it’s working: [https://xs-sol.com/testp/](https://xs-sol.com/testp/)
 * But I want to use both, Text editor and for the rest I want to use the visual
   Editor. The Javascript should not be standalone on this page. How can I fix this
   kind of bug?
 *  [Martyn Chamberlin](https://wordpress.org/support/users/martyn-chamberlin/)
 * (@martyn-chamberlin)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/simple-java-script-not-working-what-am-i-doing-wrong/#post-8177912)
 * Hi [@tanckom](https://wordpress.org/support/users/tanckom/),
 * I’m the author of Don’t Muck My Markup and I just now tested it on this theme:
   [https://wordpress.org/themes/rocked/](https://wordpress.org/themes/rocked/).
   It’s working as expected, stripping out the auto HTML. I’m curious why it’s not
   working for you. On that /testp/ page settings, have you ensured that you’re 
   checkmarking the “Disable auto-formatting for this page”?
 *  Thread Starter [tanckom](https://wordpress.org/support/users/tanckom/)
 * (@tanckom)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/simple-java-script-not-working-what-am-i-doing-wrong/#post-8178042)
 * Hey [@martyn-chamberlin](https://wordpress.org/support/users/martyn-chamberlin/),
 * So, I did this now:
    Uninstalled Don’t Muck My Markup Installed and Activated
   it again Went to Settings and checked to Disable auto-formatting for all my pages
   Went to Pages, deleted TestP and recreated it Entered the same code in text, 
   updated page, and it worked Went to visual on TestP, and the function for the
   button is gone again.
 * All steps have been done too for Disable auto-formatting for this page
 * Nothing worked. The Visual still removes that function for the button.
 * p.s. Thank you [@anevins](https://wordpress.org/support/users/anevins/) and [@martyn-chamberlin](https://wordpress.org/support/users/martyn-chamberlin/)
   for helping me with this case
 *  [Martyn Chamberlin](https://wordpress.org/support/users/martyn-chamberlin/)
 * (@martyn-chamberlin)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/simple-java-script-not-working-what-am-i-doing-wrong/#post-8178125)
 * Ah, right. Don’t Muck My Markup does not ensure that visual mode will not distort
   what you had in plain mode. All it does is ensure that whatever is in either 
   mode when you hit Update is exactly what is rendered on the page. So, tabbing
   between the two modes can definitely mess up JavaScript.
 * To solve your problem, you have a couple of solutions:
 * 1. Deactivate the Visual mode, as per Andrew’s recommendation
    2. Extrapolate
   the JavaScript code into a dedicated file that you then enqueue via your theme’s
   functions.php. A less programmatic way would be to handle this via a plugin such
   as this one: [https://wordpress.org/plugins/simple-embed-code/](https://wordpress.org/plugins/simple-embed-code/)
 *  Thread Starter [tanckom](https://wordpress.org/support/users/tanckom/)
 * (@tanckom)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/simple-java-script-not-working-what-am-i-doing-wrong/#post-8178221)
 * Okay thank you. Alread tried Embed code before, but was looking for an “In-Visual-
   option”. Since this is not possible, i’ll go with the Embed-Code.
 * thanks anyway for your time and help!

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Simple Java Script not working. What am i doing wrong?’ is closed to 
new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 3 participants
 * Last reply from: [tanckom](https://wordpress.org/support/users/tanckom/)
 * Last activity: [9 years, 8 months ago](https://wordpress.org/support/topic/simple-java-script-not-working-what-am-i-doing-wrong/#post-8178221)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
