We help IT Professionals succeed at work.

passing javascript params

Robert Saylor
on
I have a search box that I want javascript to open in a new window a google custom search enginne but pass the keyword from my site.

<form name="myform">
<input type="text" name="search_text" id="search_text"> <input type="button" name="search" onclick="goSearch()">
</form>

<script>
function goSearch() {
        var st = document.getElementById('search_text').value;
        window.open('https://www.google.com/cse/publicurl?cx=016143464297381313748:e13kxohrq8s' + st);
}
</script>

Firebug indicates the element ID is null. How do I capture the data for "search_text" with the onclick?
Comment
Watch Question

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Works fine here as long as you add a '&' to the query string before ' + st);
 window.open('https://www.google.com/cse/publicurl?cx=016143464297381313748:e13kxohrq8s&' + st);

Open in new window

Robert SaylorSenior Developer

Author

Commented:
I am having issues passing the element value with the on click. The window.open isn't the issue.

How do i pass the input type data using the on click?
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
I am not having any problem with your code.  If I type '123456' in the text input and click on the button, '123456' shows up at the end of the query string in the new window.
Robert SaylorSenior Developer

Author

Commented:
Thanks maybe something is preventing it i will look at my code more.
Fixer of Problems
Most Valuable Expert 2014
Commented:
This is the exact code I was using.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>goSearch test</title>
</head>
<body>
<h1>goSearch test</h1>
<form name="myform">
<input type="text" name="search_text" id="search_text"> <input type="button" name="search" onclick="goSearch()">
</form>

<script>
function goSearch() {
        var st = document.getElementById('search_text').value;
        window.open('https://www.google.com/cse/publicurl?cx=016143464297381313748:e13kxohrq8s&' + st);
}
</script>

</body>
</html>

Open in new window

Brian TaoOwner
Top Expert 2014

Commented:
Just a wild guess: is there any javascript code that disables the input?
Robert SaylorSenior Developer
Commented:
I think there is another piece of javascript that is preventing it from working. However, I did find another way.

<form id="formSearch" name="formSearch" style="display:inline" action="http://www.google.com/cse" target=_blank>
<input type="hidden" name="cx" value="016143464297381313748:e13kxohrq8s">
<input type="hidden" name="ie" value="UFT-8">
<input type="text" name="q" id="search_txt">
<input type="submit">
</form>

Untimely my goal was to collect the search string and pass it to google in a new window. This did the trick.
Robert SaylorSenior Developer

Author

Commented:
BTW, you have to apply for a "cx" string that will search a domain you setup in google custom search.
Robert SaylorSenior Developer

Author

Commented:
I ended up using the solution I found from google. The code in my solution posted is from a google search.