Bri.H.
Bri.H.

Reputation: 313

JavaScript populating and clearing <input> textbox

I got 6 "textboxex" and an Array with them. <input id="slot0" type="text" /> id from 0 to 5, also Array named "slotarray". I want arrray and textboxes to be bound slotarray[0] with input id="slot0" etc.

First i needed function that will find first empty field in array (no matter if corresponding textbox is empty - but should) and put there string (short string - shortcode like "abc" or "sp1"). This function also need to populate bound textbox with long string. If slotarray[2] == 'abc' then with the same number in ID (here be id="slot2") need to contain long string like "Abrasive Brilliant Conexant".

Here what i got

<a href="#" onclick="populate('abc','Abrasive Brilliant Conexant');"> click to populate </a>

and then function

 function populate(shortstring,longstring) {
  for (var i=0; i<6; i++) {
    if (slotarray[i] == '') {
      slotarray[i] = shortsrting;
      slotid = 'slot' + i;
      document.getElementById(slotid).value = longstring;
      break;
     }
   }
 }

With clearing at the moment of creating: ( Array('','','','','','') ), and textbox .value=''; its working as it should.

But then i figured out that i need function to clear textbox and bound array field. Not all but one specific for one clic. So instead of 6 functions i start to wrote

<a href="#" onclick="clear(0,'slot0');"> clear this field </a>

for each of textbox, with different numbers and ids ofcourse, and clearing function:

function clear(arrayid, slotid) {
  slotarray[arrayid] = '';
  document.getElementById(slotid).value = '';
}

But this function do not clearing textbox neither array. I see that textbox has text, and i know that array isn't cleared because first function works finding first empty object...

What am i doing wrong here? its definition of "empty"/"cleared" filed/textbox? maybe i need to use more complex conditions? maybe it is something else. Maybe i don't need array (i can manage to get rid of short-codes) and just make functions work only on textboxes?

Ok - i prepared jsfiddle demo with this, but even populating don't work..

http://jsfiddle.net/BYt49/11/

Upvotes: 0

Views: 407

Answers (2)

Ashish Kumar
Ashish Kumar

Reputation: 3039

Ok, whatever you have written is fine. Just change to way you call your javascript.

Here is jsfiddle: http://jsfiddle.net/BYt49/20/

Upvotes: 1

Valerio
Valerio

Reputation: 21

You can't use the keyword clear because refers to the (deprecated) function document.clear; so try to change the name of your "clear" function.

Upvotes: 2

Related Questions