<script lang="javascript">
function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}
function clearHWD() {
h.value = ""
w.value = ""
d.value = ""
}
function clearBpc() {
bpc.value = ""
}
function clearThis(myArray) {
for (var i=0; i<myArray.Length; i++) {
myArray[i].value = ""
}
}
</script>
</head>
<body>
<a href="javascript:clearThis(Array=('h','w','d'))">Clear</a>
So generally what I'm trying to do is send an array of form IDs to a javascript function and have it wipe any default values. my clearHWD() and clearBpc() functions work fine but i'm looking for a way to do it dynamically: my attempt can be seen in clearThis function. can anyone tell me where i'm doing this wrong OR whether this is even possible. Thank you much!
i guess i'm missing what you mean bdl. i've tried:
Code:
function clearThis(myArray) {
for (var i=0; i<myArray.Length; i++) {
myArray[i].value = ""
}
}
// Later on in the <body>
<a href="javascript:clearThis( new Array('h','w','d') )">Clear</a>
//and
<a href="javascript:clearThis( ['h','w','d'] )">Clear</a>
//and
<a href="javascript:clearThis(Array=('h','w','d'))">Clear</a>
..and it's still not working -- AFAIK my function is expecting an array. is there something else i have to do to clearThis() to make it accept an array?
Ah, I missed some things on the first pass. I was concerned with the example of passing an array to a function and neglected to see the 'lang' attribute, and this:
Code:
function clearThis(myArray) {
for (var i=0; i<myArray.Length; i++) {
myArray[i].value = ""
}
}
Length is not an array property, but length is.
The second problem is that you're assuming that the array elements have a value property. If you want to unset an array element, just assign the element itself to null or an empty string. In fact, this entire process could be simplified with a single assignment to the array itself, e.g.
Code:
function clearThis(myArray) {
if ( myArray.length > 0 ) {
myArray= null;
}
}
I guess the real question is, what are you trying to accomplish?
kk so this is my updated code -- still not working. and what i'm actually trying to do is put "clear" hyperlinks next to form fields and the javascript i'm trying to work out it supposed to clear the default values stored for those form fields on link click. my static functions work as desired, i was just trying to come up with passing an array of element ids to a loop that would consolidate the extraneous functions down to 1.
because the "values" for my form are being set by php and a reset button won't clear the values (i've tried). besides, by doing it the way i'm trying to do it, i can give my brother the ability to clear individual sections of the form instead of the form in it's entirety.
because the "values" for my form are being set by php and a reset button won't clear the values (i've tried). besides, by doing it the way i'm trying to do it, i can give my brother the ability to clear individual sections of the form instead of the form in it's entirety.
your wish is my command,
keep in mind that id value must be uniq in a page and page must have valid murkup.
try this:
Code:
function clearItems(elar){
var i, o;
for(i=0;i<elar.length;i++){
o = document.getElementById(elar[i]);
if(o){
o.value = '';
}
}
return true;
}
@oesxyl -- thank you very much, it works like a champ. i'm sorry i just wasn't getting it and you had to resort to all out writing it for me; i do appreciate it. in your function, you're assigning "i-0" instead of "i=0". just a heads up if you want to change it in case some other noob (besides me) comes to the forum and finds your awesome answer! Thanx again oesxyl!
@oesxyl -- thank you very much, it works like a champ. i'm sorry i just wasn't getting it and you had to resort to all out writing it for me; i do appreciate it. in your function, you're assigning "i-0" instead of "i=0". just a heads up if you want to change it in case some other noob (besides me) comes to the forum and finds your awesome answer! Thanx again oesxyl!
you are welcome,
I'm sorry, about that -, was a typo, and yes I will change it,