/    Sign up×
Community /Pin to ProfileBookmark

getElementById() and alert()

ok, i’m new to javascript and was just messing around writing some functions and stuff, and wrote this…

[code=php]
function myStuff()
{
var val1 = stats.text1.value
var val2 = stats.text2.value

if (val1==val2 && val1!=0 && val2!=0)
{
alert(“Matches”)
}

else
{
alert(“Does not match or value is 0”)
}
}
[/code]

as you can see this verifys that the value of two text fields match… but if i wanted to write the result to a text field instead of making an alert… wouldnt i replace an alert with this:

document.getElementById(“ans”).value = “Matches”;

this doenst work though… it doesnt write to the text field and i cant figure out why it wouldnt work, ive used getElementById() before and its worked fine. well, its nothing important but im just trying to learn js and i cant figure out the problem, thanks…

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Mar 02.2004 — This should do it, not sure what the problem with yours is...[code=php]<html><head><script>

function myStuff()
{
var val1 = document.getElementById('text1').value
var val2 = document.getElementById('text2').value


if (val1==val2 && val1!=0 && val2!=0)

{
document.getElementById('text3').value = "Matches!"
}

else

{
document.getElementById('text3').value = "Does not match or is 0"
}
}


</script></head>

<input name="text1">
<input name="text2">
<input name="text3" onclick="myStuff()">

</body></html>[/code]
Copy linkTweet thisAlerts:
@lahmayesauthorMar 02.2004 — yea me either, didnt think to do it that way, thanks
×

Success!

Help @lahmayes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...