Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-21-2009, 10:56 PM   PM User | #1
thepip3r
New Coder

 
Join Date: Mar 2005
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
thepip3r is an unknown quantity at this point
can you use inline arrays w/javascript?

Code:

<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!
thepip3r is offline   Reply With Quote
Old 04-21-2009, 11:06 PM   PM User | #2
bdl
Regular Coder

 
Join Date: Apr 2007
Location: Camarillo, CA US
Posts: 590
Thanks: 4
Thanked 83 Times in 82 Posts
bdl is an unknown quantity at this point
If the function accepts an array as an argument, then you don't need to assign an array, just pass one in, e.g.

Code:
var takesArray= function(a) {
  // do something with an array
}

takesArray( [1,2,3] );
takesArray( new Array(1,2,3) );
bdl is offline   Reply With Quote
Old 04-21-2009, 11:21 PM   PM User | #3
thepip3r
New Coder

 
Join Date: Mar 2005
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
thepip3r is an unknown quantity at this point
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?
thepip3r is offline   Reply With Quote
Old 04-22-2009, 12:24 AM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
before anything, fix the script tag:
Code:
<script type="text/javascript">
lang attribute means something else and language is depreciated.

best regards
oesxyl is offline   Reply With Quote
Old 04-22-2009, 12:36 AM   PM User | #5
thepip3r
New Coder

 
Join Date: Mar 2005
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
thepip3r is an unknown quantity at this point
kk so here's the entire file, still not working if someone can assist pls -- it would be greatly appreciated:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<head>

<title></title>

<script type="text/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>

<style type="text/css">

html { padding:0; margin:0; }

body { width:100%; padding:50px 50px 50px 50px; margin:0; }

a:link { text-decoration:none; color:blue; }
a:visited { text-decoration:none; color:blue; }
a:hover { text-decoration:underline; color:#F88017;}

</style>
</head>

<body>

<form method="GET" action="">
<p>Calculate Tire Height</p>
<input type="text" id="h" name="h" size="3" value="<?php if (isset($_SESSION['h']) && !empty($_SESSION['h'])) echo $_SESSION['h']; ?>" maxLength="3"> 
/ 
<input type="text" id="w" name="w" size="3" value="<?php if (isset($_SESSION['w']) && !empty($_SESSION['w'])) echo $_SESSION['w']; ?>" maxLength="3"> 
/ 
<input type="text" id="d" name="d" size="3" value="<?php if (isset($_SESSION['d']) && !empty($_SESSION['d'])) echo $_SESSION['d']; ?>" maxLength="3">
<a href="javascript:clearThis(Array('h','w','d'))">Clear</a>
<br><br>

<p>Calculate Bolt Pattern</p>
<input type="text" id="bpc" name="bpc" size="5" value="<?php if (isset($_SESSION['bpc']) && !empty($_SESSION['bpc'])) echo $_SESSION['bpc']; ?>" maxLength="5"> 
<a href="javascript:clearBpc()">Clear</a>
<br><br>

<input type="submit" value="Calculate">
</form>


</body>
</html>
thepip3r is offline   Reply With Quote
Old 04-22-2009, 12:38 AM   PM User | #6
bdl
Regular Coder

 
Join Date: Apr 2007
Location: Camarillo, CA US
Posts: 590
Thanks: 4
Thanked 83 Times in 82 Posts
bdl is an unknown quantity at this point
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?
bdl is offline   Reply With Quote
Old 04-22-2009, 12:58 AM   PM User | #7
thepip3r
New Coder

 
Join Date: Mar 2005
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
thepip3r is an unknown quantity at this point
Code:
<script type="text/javascript">
function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}

function clearHWD() {
	h.value = ""
	w.value = ""
	d.value = ""
}

function clearBpc() {
	bpc.value = ""
}

function clearAll() {
	h.value = ""
	w.value = ""
	d.value = ""
	bpc.value = ""
}

function clearThis(myArray) {
	if (myArray.length > 0) {
		for (var i=0; i<myArray.length; i++) {
			myArray[i].value = ""
		}
	}
}
//cliend side <a href="javascript:clearThis(Array('h','w','d'))">

</script>

<style type="text/css">

html { padding:0; margin:0; }

body { width:100%; padding:50px 50px 50px 50px; margin:0; text-align:center; }

a:link { text-decoration:none; color:blue; font-size:10px; }
a:visited { text-decoration:none; color:blue; font-size:10px; }
a:hover { text-decoration:underline; color:#F88017; font-size:10px; }

.input { border:1px solid 

</style>
</head>

<body>

<form method="GET" action="">
<p>Calculate Tire Height</p>
<input type="text" id="h" name="h" size="3" value="<?php if (isset($_SESSION['h']) && !empty($_SESSION['h'])) echo $_SESSION['h']; ?>" maxLength="3"> 
/ 
<input type="text" id="w" name="w" size="3" value="<?php if (isset($_SESSION['w']) && !empty($_SESSION['w'])) echo $_SESSION['w']; ?>" maxLength="3"> 
/ 
<input type="text" id="d" name="d" size="3" value="<?php if (isset($_SESSION['d']) && !empty($_SESSION['d'])) echo $_SESSION['d']; ?>" maxLength="3">
<a href="javascript:clearThis(Array('h','w','d'))">Clear</a>
<br><br>
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.
thepip3r is offline   Reply With Quote
Old 04-22-2009, 01:05 AM   PM User | #8
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
why don't you use a input reset instead of this?
Code:
<input type="reset" value="Clear all">
best regards
oesxyl is offline   Reply With Quote
Old 04-22-2009, 04:02 PM   PM User | #9
thepip3r
New Coder

 
Join Date: Mar 2005
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
thepip3r is an unknown quantity at this point
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.
thepip3r is offline   Reply With Quote
Old 04-22-2009, 04:21 PM   PM User | #10
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by thepip3r View Post
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;
}
and replace anchor with button:
Code:
<input type="button" value="Clear Items" onclick="return clearItems(['h','w','d'])">
add how many buttons you need but change value and array parameters from onclick.

best regards

Last edited by oesxyl; 04-22-2009 at 04:44 PM..
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
thepip3r (04-22-2009)
Old 04-22-2009, 04:38 PM   PM User | #11
thepip3r
New Coder

 
Join Date: Mar 2005
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
thepip3r is an unknown quantity at this point
@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!
thepip3r is offline   Reply With Quote
Old 04-22-2009, 04:43 PM   PM User | #12
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by thepip3r View Post
@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,

best regards
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:29 PM.


Advertisement
Log in to turn off these ads.