...

View Full Version : Help using GetElementById!



metomeya
10-01-2008, 08:02 PM
I'm a student taking a CIS class, so I hope this isn't too basic of a question for ya.

Anyways I'm trying to have an alert popup box come up when you don't enter your name in an input and keep that person from going to the next page.

I can get an alert message to pop-up
I can get it to keep form going to my next website
I can get it to use if statement with vars i define in it like (var test = 0; if (test==0) {blah blah})
But I can't get it to use firstname in the if statement.

Heres part of my code for my Confirm.js:


// JScript File
function valid()
{
var fname = document.getElementById("firstname");

if (fname ==" ")
{
alert("You did not fill in your first name");
return false;
}
}


Heres part of my code for my Default.aspx:


<head>
<script src="Confirm.js" type="text/javascript"></script>
</head>

<body>
<form id="timeform" action="none.aspx" method="post">
First name: <input type="text" name="firstname"/>
<input type="submit" value="Clock In/Out" onclick="return valid()" />
</form>
</body>


Is there something i'm doing wrong here?

Thanks

A1ien51
10-01-2008, 08:13 PM
Well you have no id. You have to assign the element an id.



Eric

Kor
10-01-2008, 08:40 PM
name and id are different attributes. Your element has only an name, but no id.

metomeya
10-01-2008, 08:54 PM
<head>
<script src="Confirm.js" type="text/javascript"></script>
</head>

<body>
<form id="timeform" action="none.aspx" method="post">
First name: <input type="text" id="firstname" name="other" />
<input type="submit" value="Clock In/Out" onclick="return valid()" />
</form>
</body>


Okay I did that, (actually i had tried that before), but it still doesn't work.....

I'm using Visual Studio 2005 if that helps.

ess
10-01-2008, 09:51 PM
[CODE]
I'm using Visual Studio 2005 if that helps.

If you are using a MasterPage, sometimes the generated IDs of html tags are combined with those in the MasterPages.

Even if you weren't using a MasterPage, I would look at the generated HTML to see if there are different from what you entered....just in case.

Cheers
~E

metomeya
10-01-2008, 10:10 PM
Well, like I said, I really new to this programming stuff.

There is the code behind the aspx file.




public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
}


Would that Page have anything to do with the master page?

If so how do I link it all up?

metomeya
10-02-2008, 02:14 AM
Okay so I emailed the TA.

I needed to make

var fname = document.getElementById("firstname")

into

var fname = document.getElementById("firstname").value

Kor
10-02-2008, 10:40 AM
asp (php, java, jsp...) has no relevance upon javascript (unless asp generates a javascript code, which is not important in this case). For javascript is important only the HTML generated document, thus you must analyze the HTML (what you see when View Source)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum