954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

using value of textfield as parameter

I know this is a basic question. I asked the same question a while ago, even then thinking it was a simple question. The answer I got seemed to be what I already know and was obvious.. however, when I tried it, it did not work, so I just worked around it for a while... But now the same question is coming up, and this method is still not working...

<p>Name : <input type="text" id="theName" name="theName" value=""></p>
<p><input type="button" name="btnGo" value="Go" onClick="goNow(document.getElementByID('theName').value)"></p>


that is what I currently have, and for some reason it is not working... am I doing something wrong, or why is it not working?

FALL3N
Junior Poster in Training
84 posts since May 2010
Reputation Points: 10
Solved Threads: 2
 

And what do you get if you have this:

<p>Name : <input type="text" id="theName" name="theName" value=""></p>
<p><input type="button" name="btnGo" value="Go" onClick="alert(document.getElementById('theName').value);"></p>


Also getElementById not getElementByID (case sensitive)

stbuchok
Master Poster
730 posts since May 2011
Reputation Points: 120
Solved Threads: 93
 

I know this is a basic question. I asked the same question a while ago, even then thinking it was a simple question. The answer I got seemed to be what I already know and was obvious.. however, when I tried it, it did not work, so I just worked around it for a while... But now the same question is coming up, and this method is still not working...

<p>Name : <input type="text" id="theName" name="theName" value=""></p>
<p><input type="button" name="btnGo" value="Go" onClick="goNow(document.getElementByID('theName').value)"></p>

that is what I currently have, and for some reason it is not working... am I doing something wrong, or why is it not working?


What's not working?!!

Troy III
Practically a Master Poster
609 posts since Jun 2008
Reputation Points: 120
Solved Threads: 80
 

remove value attribute from input tag
also try

document.getElementById('some_id').getValue()
baig772
Junior Poster
123 posts since Mar 2011
Reputation Points: 29
Solved Threads: 6
 

There is no problem with having value in there. Also getValue() is not even a valid method, it throws an error.

<html>
	<head>
		<title></title>
	</head>

	<body>
		<form>

		Name:<input type="text" id="txtName" value="" />
		<input type="button" id="btnGo" value="Go" onclick="alert(document.getElementById('txtName').value);" />

		</form>
	</body>
</html>


Troy III is still right though, what is not working?

stbuchok
Master Poster
730 posts since May 2011
Reputation Points: 120
Solved Threads: 93
 

I know this is a basic question. I asked the same question a while ago, even then thinking it was a simple question. The answer I got seemed to be what I already know and was obvious.. however, when I tried it, it did not work, so I just worked around it for a while... But now the same question is coming up, and this method is still not working...

<p>Name : <input type="text" id="theName" name="theName" value=""></p>
<p><input type="button" name="btnGo" value="Go" onClick="goNow(document.getElementByID('theName').value)"></p>

that is what I currently have, and for some reason it is not working... am I doing something wrong, or why is it not working?

This is your code

<p>Name : <input type="text" id="theName" name="theName" value=""></p>
<p><input type="button" name="btnGo" value="Go" onClick="goNow(document.getElementByID('theName').value)"></p>

What's it supposed to do, because the code provided is absolutely unclear at this point. It doesn't unveil your aim nor what you are aiming to achieve with it
Let's first correct your spelling error on the method first:

<p>Name : <input type="text" id="theName" name="theName" value=""></p>
<p><input type="button" name="btnGo" value="Go" onClick="goNow(document.getElementById('theName').value)"></p>


Now, even if your "goNow" would happen to be the name of some well written function, it should return nothing since the value of your "theName" input is empty by default.
At this point, the "goNow()" [name of some presumed existing function] doesn't tell us much -not even after giving it some seriously deep dark thoughts.
We are after all ordinary coders; some are good, some are better, but in either case -we're no psychics.

Troy III
Practically a Master Poster
609 posts since Jun 2008
Reputation Points: 120
Solved Threads: 80
 

I really appreciate all your answers, but I think I now know why it is not working.. not sure tho..

this code is for a 'widget' that appears on the homepage of a free hosting site... you enter HTML/javascript and build your custom widget the way you want it.. But the classic DOM structure may not apply... I guess the 'correct' code would be something like:

<p>Name : <input type="text" id="theName" name="theName" value=""></p>
<p><input type="button" name="btnGo" value="Go" onClick="goNow(widget.getElementById('theName').value)"></p>


that's only a guess, but its probably something like that...

assuming that's right (and let me know if it is not)

should I
a) try to access the 'document' of the widget using some roundabout way (possibly with metatags?)

OR

b) there is no simple, all-encompassing way of performing the above, and I should ask the hosting site specifically how to access the widget and it's elements?

FALL3N
Junior Poster in Training
84 posts since May 2010
Reputation Points: 10
Solved Threads: 2
 

Why you're trying to pass the value as function argument. If you know the ID of the field, then, you can easily get the value within the function.
Example:

function goNow(){
    var input = document.getElementById('theName').value;
    // do your statement
}

Binding event that function without argumentonClick="goNow()"
It should work.

Zero13
Practically a Master Poster
624 posts since Jan 2009
Reputation Points: 120
Solved Threads: 139
 

that's a very good point, I'll do that, thanks!

FALL3N
Junior Poster in Training
84 posts since May 2010
Reputation Points: 10
Solved Threads: 2
 

Sorry, but that isn't a good point. Your function will be tightly coupled with the items on the page. Passing in the value makes this less coupled and easier to extend to other parts of your application without modification.

Pass in the value.

Or

<a href="" onclick="goNow('elementId');" ...



function goNow(elementId){
    var element = document.getElementById(elementId);

    ...
}


But I still prefer passing in the value than the id of the element.

stbuchok
Master Poster
730 posts since May 2011
Reputation Points: 120
Solved Threads: 93
 

ahh, yes, that is a better programming practice, thank you!

FALL3N
Junior Poster in Training
84 posts since May 2010
Reputation Points: 10
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: