views:

16

answers:

1

Hi,

When I add text to a div in a chrome extension popup, the text appears for a fraction of a second and then immediately dissappears again.

This is a small example:

<html>
  <head>
    <script>
      function show() {
        var newName = document.showMe.textToShow.value;
        var txt = document.createTextNode(newName);
        document.getElementById("feedback").appendChild(txt);
      }
    </script>
  </head>
  <body>
    <form name="showMe">
      Name: <input type="text" name="textToShow" /><br />
      <input type="submit" value="Show" OnClick="show()" />
    </form>
    <div id="feedback"></div>
  </body>
</html>

What am I doing wrong?

Thanks,

Miel.

+1  A: 

It works, but then the form gets submitted. You have to suppress sending the form like this

<input type="submit" value="Show" OnClick="show(); return false" />

or use a button:

<button type="button" onclick="show()">Show</button>
thejh
Thanks, that does the trick. I was wondering already why the debugger made me step through the `</form>` twice.
Miel