By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Getting event.target attributes

P: n/a
I have a web form with several "submit" buttons.

One of these uploads a file, and I want to validate the user input
prior to submitting the form. I have an event handler specified with
the form's "onsubmit" attribute which checks the the
event.target.value to make sure the right submission type is being
evaluated.

When I try to get the target or srcElement value from the event
object, however, it is always "undefined".

Here is the cut-down version of the handler...

function validateIfUpload() {
if (window.event) {
evt = window.event
} else if (document.event) {
evt = document.event
}
if (evt.target) targ = evt.target
else if (evt.srcElement) targ = evt.srcElement
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode
if (targ.value == "Upload") {
return validateUpload('prod_img_f')
}
return true
}

Mar 5 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Mar 5, 7:53 am, "crater" <iccar...@gotadsl.co.ukwrote:
I have a web form with several "submit" buttons.

One of these uploads a file, and I want to validate the user input
prior to submitting the form. I have an event handler specified with
the form's "onsubmit" attribute which checks the the
event.target.value to make sure the right submission type is being
evaluated.

When I try to get the target or srcElement value from the event
object, however, it is always "undefined".

Here is the cut-down version of the handler...

function validateIfUpload() {
if (window.event) {
evt = window.event
} else if (document.event) {
evt = document.event
}
if (evt.target) targ = evt.target
else if (evt.srcElement) targ = evt.srcElement
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode
if (targ.value == "Upload") {
return validateUpload('prod_img_f')
}
return true

}
what browser are you using? seems like you're suffering from a bout
of browser incompatibilities among a few coding errors. be careful,
the event is not always handled as a global object, as you have taken
for granted. try passing it through your procedure call for
onsubmit.

<form name="form1" onsubmit="validateIfUpload(event)">

next you will have to change your validateIfUpload method:

function validateIfUpload(evt) {
evt = evt || window.event;
var targ = evt.target || evt.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode
if (targ.value == "Upload") {
return validateUpload('prod_img_f')
}
return true;
}

i tried this in both ie7 and Fx2.0 and it worked. Not sure how safari
will handle it. hope that helps a little.

good luck
Mar 5 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.