0

I use bootstrap and have a simple layout including a button. I tried the code solution from here but it is not working. This is my code, it is not even showing 'test' after clicking the search button.

    <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Die drei oberen meta tags *müssen* zuerst im Head-Tag angegeben werden; alle anderen Tags können anschließend eingefügt werden -->
  <title>Search</title>

  <!-- Bootstrap -->
  <link href="css/bootstrap.min.css" rel="stylesheet">

  <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
      <![endif]-->
    </head>
    <body>
    <div class="container-fluid">
      <h1>Welcome!</h1>
    </br>
    <button id="search" type="button" class="btn btn-primary">Suchen</button>
    <p><br/></p>
    <p id="output"></p>
    </div>
    <script type="text/javascript">
      $('#search').on('click', function (e) {
        document.getElementById('#output').innerHTML = "test";

        var EventSearch = require("facebook-events-by-location-core");

        var es = new EventSearch({
          "lat": 40.710803,
          "lng": -73.964040
        });

        es.search().then(function (events) {
          console.log(JSON.stringify(events));
          document.getElementById('output').innerHTML = JSON.stringify(events);
        }).catch(function (error) {
          console.error(JSON.stringify(error));
        }); 
      })
    </script>

    <!-- jQuery (notwendig für Bootstraps JavaScript plugins) -->
    <script src="js/jquery-3.2.1.min"></script>
    <!-- Alle kompilierten plugins einbeziehen -->
    <script src="js/bootstrap.min.js"></script>
  </body>
  </html>
Community
  • 1
  • 1
10jo10
  • 27
  • 1
  • 3
  • 5

2 Answers2

0

When using document.getElementByid(), you dont need the # before the id.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Die drei oberen meta tags *müssen* zuerst im Head-Tag angegeben werden; alle anderen Tags können anschließend eingefügt werden -->
  <title>Search</title>

  <!-- Bootstrap -->
  <link href="css/bootstrap.min.css" rel="stylesheet">

  <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
      <![endif]-->
    </head>
    <body>
    <div class="container-fluid">
      <h1>Welcome!</h1>
    </br>
    <button id="search" type="button" class="btn btn-primary">Suchen</button>
    <p><br/></p>
    <p id="output"></p>
    </div>
    <script type="text/javascript">
      $('#search').on('click', function (e) {
        document.getElementById('output').innerHTML = "test";
        // I removed the event search code because I do not have the files and it was breaking the example.
      })
    </script>

    <!-- jQuery (notwendig für Bootstraps JavaScript plugins) -->
    <script src="js/jquery-3.2.1.min"></script>
    <!-- Alle kompilierten plugins einbeziehen -->
    <script src="js/bootstrap.min.js"></script>
  </body>
  </html>
Ken
  • 466
  • 2
  • 7
  • If I run the code snippet in stackoverflow it works, but if I run the same from sublimetext it does not work. – 10jo10 Apr 26 '17 at 16:33
  • If this is your only code, it looks like you are not including jQuery, which would make $('#search').on(... break. – Ken Apr 26 '17 at 19:09
  • Thank you for your answer. But I am saying , isn't this including jquery? Path and filename are also correct. – 10jo10 Apr 26 '17 at 19:20
  • Oh, yes, that would include them. I missed them because they are below the rest of your code. What is the error message in the console when you run this locally? – Ken Apr 26 '17 at 19:41
  • It says 'Uncaught ReferenceError: $ is not defined' and points at this line: $('#search').on('click', function (e) { – 10jo10 Apr 26 '17 at 20:16
  • Thank you all for your help, I finally found the error, I had to include jquery at the top of my . It was used before it was included. – 10jo10 Apr 26 '17 at 20:26
0

First of all, don't use the "#" sign when using document.getElementById. Secondly, you need to add the "require" reference.

Check the console message:

 $('#search').on('click', function (e) {
        document.getElementById('output').innerHTML = "test";

        var EventSearch = require("facebook-events-by-location-core");

        var es = new EventSearch({
          "lat": 40.710803,
          "lng": -73.964040
        });

        es.search().then(function (events) {
          console.log(JSON.stringify(events));
          document.getElementById('output').innerHTML = JSON.stringify(events);
        }).catch(function (error) {
          console.error(JSON.stringify(error));
        }); 
      })
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container-fluid">
      <h1>Welcome!</h1>
    </br>
    <button id="search" type="button" class="btn btn-primary">Suchen</button>
    <p><br/></p>
    <p id="output"></p>
    </div>
Amir H. Bagheri
  • 1,416
  • 1
  • 9
  • 17
  • If I comment out the search part it still does not work (it does not show 'test') from my sublime text editor. – 10jo10 Apr 26 '17 at 16:34