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?

Recommended Answers

All 10 Replies

Member Avatar for stbuchok

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)

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?!!

remove value attribute from input tag
also try

document.getElementById('some_id').getValue()
Member Avatar for stbuchok

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?

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.

commented: I'm starting to appreciate your humor +7

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?

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 argument onClick="goNow()"
It should work.

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

Member Avatar for stbuchok

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.