Jump to content

Gameboyz's Javascript Queries


gameboyz

Recommended Posts

I don't think I quite understand what you want to do. You can set a variable to reference a DOM node such as a form field, but that function is set up to operate with() the form field so it would be superfluous to do so.And remember only one element on a page can have a certain id. It is a one-to-one relationship.

Link to comment
Share on other sites

guys im really lost at the http://www.w3schools.com/js/js_form_validation.asp 1) why isn't it possible to use a variable to define the field name? eg:
<html><head><script type="text/javascript">function validate_required(field,alerttxt){with (field){if (value==null||value=="")  {alert(alerttxt);return false;}else {return true}}}function validate_form(thisform){var email2="email";with (thisform){if (validate_required(email2,"Email must be filled out!")==false)  {email.focus();return false;}}}</script></head><body><form action="submitpage.htm"onsubmit="return validate_form(this)"method="post">Email: <input type="text" name="email" size="30"><input type="submit" value="Submit"> </form></body></html>

2) is there a way to code it such that the field name checks every field of a specific id?

Hi, under the validate_form() function I declared variable email2 as "email" however the script doesn't work when I use the variable instead of "email". And oh, my bad, I mean a specific class, say "class="required"".
Link to comment
Share on other sites

If you think about it, that function isn't set up to use strings as the element to check. If we replace all the field references with "email" in validate_required(), we get

with ("email"){if (value==null||value=="")  {alert(alerttxt);return false;}else {return true}}

which obviously won't work.The className property checks what class an element has.

Link to comment
Share on other sites

Oh yea and why is it that this works:

<html><head><script type="text/javascript">function validate_required(field,alerttxt){with (field){if (value==null||value=="")  {alert(alerttxt);return false;}else {return true}}}function validate_form(thisform){var x=document.getElementById("hi");with (thisform){if (validate_required(email,"Email must be filled out!")==false)  {email.focus();return false;}}}</script></head><body><form action="submitpage.htm"onsubmit="return validate_form(this)"method="post">Email: <input type="text" name="email" size="30"><input type="submit" value="Submit"></form></body></html>

but this doesn't

<html><head><script type="text/javascript">function validate_required(field,alerttxt){with (field){if (value==null||value=="")  {alert(alerttxt);return false;}else {return true}}}function validate_form(thisform){var x=document.getElementByName("hi");with (thisform){if (validate_required(email,"Email must be filled out!")==false)  {email.focus();return false;}}}</script></head><body><form action="submitpage.htm"onsubmit="return validate_form(this)"method="post">Email: <input type="text" name="email" size="30"><input type="submit" value="Submit"></form></body></html>

Link to comment
Share on other sites

There is no getElementByName() method. There is, however, a getElementsByName() method. It returns a node list, like getElementsByTagName(). It is only useful for form elements, which are the only elements strictly permitted to have names. It returns a list because elements like radio buttons or checkboxes can all have the same name.Even though you're not doing anything with your return value (x) in those calls, calling an unknown function halts execution. So the one script works and the other does not.

Link to comment
Share on other sites

Why won't this work

<body><script type="text/javascript">function predict(){if (document.getElementById("predict").value == '') { document.getElementById("predict").value = document.getElementById("myform").elements['firstname'].value + ' ' + document.getElementById("myform").elements['lastname'].value}}</script><form id="myform"><input name="firstname"><input name="lastname"><input id="predict" onfocus="predict()"></form></body>

but this will work

<body><form id="myform"><input name="firstname"><input name="lastname"><input id="predict" onfocus="if (this.value == '') { this.value = this.form.elements['firstname'].value + ' ' + this.form.elements['lastname'].value}"></form></body>

:)

Link to comment
Share on other sites

I see this sometimes. It's pretty random. Every time I come up with an explanation that seems to make sense, the next case proves my explanation wrong. Anyway, try changing the function name. If that doesn't work, try pasting the old code under a new function declaration.Having a function with the same name as the id of a page element does not seem to violate any rules I'm aware of, but I do try to use different names/ids for things.

Link to comment
Share on other sites

I see this sometimes. It's pretty random. Every time I come up with an explanation that seems to make sense, the next case proves my explanation wrong. Anyway, try changing the function name. If that doesn't work, try pasting the old code under a new function declaration.Having a function with the same name as the id of a page element does not seem to violate any rules I'm aware of, but I do try to use different names/ids for things.
Yea you're right :) changing the function name to predicter works like a charm :)
Link to comment
Share on other sites

  • 3 months later...

Hey why is it okay if I inserted a function that will be triggered upon onload() that will cause an alert(), but not if I inserted a function that will be triggered upon onload() which is more complicated like, change the text of an element (innerHTML)?

Link to comment
Share on other sites

Because your code is messed up? It doesn't matter what code you have in a function, it will run on page load. If the function isn't doing what you expect then I would say the code isn't correct.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...