Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    30
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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!

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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) );

  • #3
    New Coder
    Join Date
    Mar 2005
    Posts
    30
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

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

    best regards

  • #5
    New Coder
    Join Date
    Mar 2005
    Posts
    30
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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>

  • #6
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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?

  • #7
    New Coder
    Join Date
    Mar 2005
    Posts
    30
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    why don't you use a input reset instead of this?
    Code:
    <input type="reset" value="Clear all">
    best regards

  • #9
    New Coder
    Join Date
    Mar 2005
    Posts
    30
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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 05:44 PM.

  • Users who have thanked oesxyl for this post:

    thepip3r (04-22-2009)

  • #11
    New Coder
    Join Date
    Mar 2005
    Posts
    30
    Thanks
    2
    Thanked 0 Times in 0 Posts
    @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!

  • #12
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •