0

I'm trying to get some simply things going but can't seem to get a JS function to run following a click event.

Relevant HTML and JS:

function addSite() {
  alert("DO SOMETHING");
}
<div id="whitelist" class="row-fluid">
  <div class="span12">
    <p>Enter the sites you use to work:</p>
    <form action="">
      <input class="input-mini" type="text" id="add-site" />
      <input type="submit" onclick="addSite" />
    </form>
    <p>Your Whitelist:</p>
    <div id="show-sites"></div>
  </div>
</div>

Full HTML and JS here and here.

Mr. Polywhirl
  • 31,606
  • 11
  • 65
  • 114
Sank Finatra
  • 144
  • 1
  • 9

2 Answers2

0

function addSite()
{
    alert("DO SOMETHING");
}
<div id="whitelist" class="row-fluid">
        <div class="span12">
            <p>Enter the sites you use to work:</p>
            <form action="">
                <input class="input-mini" type="text" id="add-site" />
                <input type="submit" onclick="addSite()" />
            </form>
            <p>Your Whitelist:</p>
            <div id="show-sites"></div>
        </div>
    </div>

you need to include the parenthesis when you call the function, which is missing in your html.

sumit chauhan
  • 1,185
  • 1
  • 11
  • 18
  • I followed your suggestion but it still doesn't work...hmmm the rabbit hole goes deeper. I will look through the code more and report back. – Sank Finatra Jul 17 '17 at 18:09
  • 3
    Chrome extensions pages don't allow inline JavaScript. [onClick within Chrome Extension not working](//stackoverflow.com/a/13592045) – wOxxOm Jul 17 '17 at 18:22
0

It should be

onclick="addSite()"

instead of

onclick="addSite"

EDIT:

As @wOxxOm pointed out, you should not use not onclick attribute to fire a event when working with chrome extension. Instead bind event listener to that particular DOM element using addEventListener.

function addSite() {
  alert("DO SOMETHING");
}
<div id="whitelist" class="row-fluid">
  <div class="span12">
    <p>Enter the sites you use to work:</p>
    <form>
      <input class="input-mini" type="text" id="add-site" />
      <input type="submit" onclick="addSite()" />
    </form>
    <p>Your Whitelist:</p>
    <div id="show-sites"></div>
  </div>
</div>

Since you have set action attribute to empty string, do read this:

In HTML5, you can actually specify an action on the submit button itself. If there isn't one, it uses the form's action and if that is not set, it defaults to the empty string (note you cannot explicitly set the action to an empty string in HTML5).

Ref.: Is action really required on forms?

vatz88
  • 2,132
  • 2
  • 13
  • 21
  • I followed your suggestion but it still doesn't work...hmmm the rabbit hole goes deeper. I will look through the code more and report back – Sank Finatra Jul 17 '17 at 18:10
  • 3
    Chrome extensions pages don't allow inline JavaScript. [onClick within Chrome Extension not working](//stackoverflow.com/a/13592045) – wOxxOm Jul 17 '17 at 18:22
  • Attach a event listener, don't use `onclick` attribute. – vatz88 Jul 17 '17 at 18:24