0

I am trying to make this code work, I want the innerHTML to be changed but It's instantly changing and gets back to its initial state . Any help ?

<!DOCTYPE html>
<html>
    <head>
        <script>
            function click_me(){ 
                alert('fdsfs');
                var tweety=document.getElementById('text_this').innerHTML;
                alert(tweety);
                var text_area="<input type=text value='"+tweety+"'>";
                alert(text_area);
                document.getElementById('text_this').innerHTML=text_area; 
             }
        </script>
    </head>
<body>
    <form name="form1">
        <table border=1>
            <tr>
                <td>
                    <div id="text_this">123</div>
                </td>
                <td>
                    <button onclick="click_me();">Edit</button>
                </td>
            </tr>
</body>
</html>

2 Answers 2

4

Your form is getting submitted. Add a return false; to your button onclick event.

<button onclick="click_me(); return false;">Edit</button>

Or, make the button type='button' (This is the better option)

<button onclick="click_me();" type='button'>Edit</button>

The reason is because the button type is submit by default if a type is not specified.

From MDN,

submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.

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

Comments

1
<!DOCTYPE html>
<html>
  <head>
    <script>
    function click_me(){ 
      alert('fdsfs');
      var tweety=document.getElementById('text_this').innerHTML;
      alert(tweety);
      var text_area="<input type=text value='"+tweety+"'>";
      alert(text_area);
      document.getElementById('text_this').innerHTML=text_area; 
    }
    </script>
  </head>
  <body>
    <form name="form1">
      <table border=1>
        <tr>
          <td><div id="text_this">123</div></td>
          <td><button onclick="click_me();return false;">Edit</button></td>
        </tr>
      </table>
    </form>
  </body>
</html>

added a return false statement to the onclick event. without it, the form gets submitted and the page reloads

jsfiddle here

8 Comments

I don't believe a <button> does submit the page.
Buttons' actions are browser-specific. Some do submit forms: w3schools. It's better to use <input> tags in that case.
@Rogue: Yes, default seems to be submit. I have posted the MDN link and content in my answer above.
@11684: Strange, but in codepen even if I add a form it is not getting submitted but in jsFiddle it does. There must be some difference between those two. But I still think MDN wouldn't be wrong.
@11684—while MDN isn't wrong about that, it's just a community Wiki and is wrong and in need of update about some things.
|

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.