2 javascript questions

J

Jeff User

Hi
Sorry if this is kind of long, it is myhope that it is clear and
staright forward.

I am totaly new to javascript.
I figured out this much.
First, I wanted to clear the contents of a label on the form when the
user clicks the submit button (btnCreate).
Here is what I have

In page_Load I do this:

btnCreate.Attributes.Add("onclick", "clearGeneral()");

and then in html/asp code I place script function (in head):

function clearGeneral(){
//Clear text in label
document.getElementById("lblGeneralMessage").innerText = "";
}
......

It works fine and the page posts back as there are , of course, other
things to be taken care of on server side.
Then, I wanted to do "simple" validation of a asp.net1.1 textbox
control to check if it is empty, on the same event.
So I added another Attribute to the button and it also seems to work:

btnCreate.Attributes.Add("onclick", "Val_create()");

and with asp/html script like this:

function Val_create(){
if (document.Form1.txtNewDbName.value == "")
{
alert("New name field can not be empty");
}
}

Here are my 2 questions:
1) What is the difference between
document.getElementById("lblGeneralMessage").innerText
and
document.Form1.txtNewDbName.value
(Other than one is a label and one is textbox, unless that affects the
answer)

Because
When first using
document.getElementById("lblGeneralMessage").innerText = "";
to refer to the text of a label it works (I can refer to text), but
when I try that to compare the value of a textbox, it always evaluated
toTRUE ->

document.getElementById("txtNewDbName").innerText == "";

^that is always true, so had to use
document.Form1.txtNewDbName.value == "";
and this works OK

2) Is there a way that I can stop the page from posting back if
if (document.Form1.txtNewDbName.value == "")
is true? In addition to the alert, I do not need the page to post back
as there is no data to work with.

Regards
Jeff
 
K

Kevin Spencer

document.GetElementById() refers to the id attribute of an HTML element in
the document. document.FormName.ElementName refers to the name attribute of
an element in a form having the name "FormName."

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 
E

Eliyahu Goldin

Q2. Yes, there are 2 ways you can try
1. btnCreate.Attributes.Add("onclick", "return Val_create();");

and with asp/html script like this:

function Val_create(){
if (document.Form1.txtNewDbName.value == "")
{
alert("New name field can not be empty");
return false
}else return true;
}

2. btnCreate.Attributes.Add("onclick", "Val_create()");

and with asp/html script like this:

function Val_create(){
if (document.Form1.txtNewDbName.value == "")
{
alert("New name field can not be empty");
window.event.cancelBubble=true;
}

Eliyahu
 
J

Jeff User

Thanks to all

Eliyahu, I went with option 1 because it was first and it works like a
charm.
Thanks again
Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top