0

I am making a extension theme for my Chromebook that searches coding sites (like this site, w3schools, ect.) How sould I make it? This is my code so far:

<html>
<head>
</head>
<body>    
<input id="input1">
<button onclick="searchGoogle()">Search Google</button>
<script>
function searchGoogle() {
        var one = document.getElementById("input1").value;
        var two = 'http://www.google.com/search?q=' + one;
        window.location = two;
    }
</script>
</body>
</html>

My code doesn't work

When it runs, this pops up:

(Image of my code running)

Is my code wrong?

Any help will be aapreciated.

EDIT

<html>
<head>
<script src="searchgoogle.js">
</script>
</head>
<body>    
<input id="input1">
<button id="link">Search Google</button>

</body>
</html>

and

document.addEventListener('DOMContentLoaded', function() {
    var link = document.getElementById('link');
    // onClick's logic below:
    link.addEventListener('click', function() {

        function searchGoogle() {
        var one = document.getElementById("input1").value;
        var two = 'https://www.google.com/search?q=' + one;
        window.location = two;
    }


    });
});

Didn't work either

Ethan P
  • 82
  • 8

1 Answers1

1

You declare the function searchGoogle inside the listener function but it is never called. Try with:

document.addEventListener('DOMContentLoaded', function() {
    var link = document.getElementById('link');
    // onClick's logic below:
    link.addEventListener('click', function() {

        //there is no need to declare a new function here.

        var one = document.getElementById("input1").value;
        var two = 'https://www.google.com/search?q=' + encodeURIComponent(one);
        window.location = two;



    });
});
Iván Nokonoko
  • 4,088
  • 2
  • 11
  • 19