Can't get value from textfield?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Can't get value from textfield?

Post by oscardog »

Well I've used this code a million times before and for some reason it's not working. Below is the form. I have tried the JS in both the head and in the body of the page.

Code: Select all

<script type="text/javascript">
function searchFunc() {
var searchString = document.getElementByName("searchString").value;
alert(searchString);
window.location = "http://www.mysite.com/webdesignblog/search/" + searchString + "/";
}
</script>

Code: Select all

<form action="javascript:searchFunc()" name="searchform" id="searchform" method="get">
<input type="text" id-"searchString" name="searchString" /><input type="submit" name="action" value="Search" onClick="this.submit"/>
</form>
The JS function is being called. I just put an alert into the function with all other lines of code commented out and an alert appeared. As soon as I include the line that gets the value from the textfield nothing happens.

Any ideas?
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Can't get value from textfield?

Post by PHPHorizons »

Hello oscardog,

Perhaps the problem is the html syntax error: id-"searchString"
Note the minus sign instead of an =

Cheers
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Can't get value from textfield?

Post by oscardog »

PHPHorizons wrote:Hello oscardog,

Perhaps the problem is the html syntax error: id-"searchString"
Note the minus sign instead of an =

Cheers
Oh wow. If it is that I'll be a little annoyed, I spent a while looking at it trying to figure it out. Will try it out on Monday and let you know if it fixes it :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Can't get value from textfield?

Post by John Cartwright »

There is no such function as document.getElementByName(), what you probably want is document.getElementById()
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Can't get value from textfield?

Post by PHPHorizons »

John wrote:There is no such function as document.getElementByName()
But there is a document.getElementsByName method. I don't know if oscardog meant to use the document.getElementsByName() method. Upon looking back over oscardog's code though, the function was misspelled by not adding an s to Elements.


I agree that getElementById() would be the better choice there though.

Cheers
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Can't get value from textfield?

Post by oscardog »

John Cartwright wrote:There is no such function as document.getElementByName(), what you probably want is document.getElementById()
Yeh I know, a typing error.

I had been using document.getElementById() on most of the attempts but as that wasnt' working I decided to try the other (which clearly wouldn't work if I mispelt it!).

Anyway I'll correct all these things on Monday and try it out :)

Cheers for the help!
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Can't get value from textfield?

Post by oscardog »

Just thought I would let you guys know it did fix it. Correcting the typo from a hyphen to an equals sign fixed it :)
Post Reply