Good all the time of day.

There is a page, a form and several buttons are stuck on it. The buttons look like this:

<a class="ttf02" name="p31k01" onClick="document.form.z13.value = '01'">01</a> <a class="ttf02" name="p31k02" onClick="document.form.z13.value = '02'">02</a> ... <a class="ttf02" name="p31k50" onClick="document.form.z13.value = '50'">50</a> 

Actually, you can guess that when you click on input, the name = "z13" value is replaced with the name of the link. Is it possible in some way, when pressed once to enter data into the input, 2 times to delete, while having several values? Sort of:

 // жмем ссылку 03, 02, 15 <input name="z13" value"03 02 15 "> // нажимаем еще раз ссылку 02 <input name="z13" value"03 15 "> // нажимаем ссылку 05, 07, 15 <input name="z13" value"03 05 07 "> 

I saw something similar on JS and JQ, so I wondered if I could do without them. Even as an option, I consider the "msie knows sense in perversion" option: next to each link is the invisible input name = "a01, a02 ... a50", in which the value appears or disappears, and in the input name = "z13" only those which are not "" (empty).

  • in a sense without js? You set the value for JS: onClick = "document.form.z13.value = '01'" - Yura Ivanov
  • Now I will write a script, a moment! - Palmervan
  • uh ... a little wrong with the wording. without JS, in the sense of a solution, seen on the Internet could barely fit into RAM. I have nothing against something simple on JS - sergey
  • Regular search and replace for text replacement, I won’t put my mind on what may be voluminous in terms of memory ... - Zowie

4 answers 4

html:

 <a class="ttf02" href="#" name="p31k01" onClick="return setV('01');">01</a> <a class="ttf02" href="#" name="p31k02" onClick="return setV('02');">02</a> <a class="ttf02" href="#" name="p31k50" onClick="return setV('50');">50</a> <br/> <form> <input name="z13" value=""> </form> 

script:

 var setV = function(v) { var values = document.forms[0].z13.value.split(' '); var idx = values.indexOf(v); if (idx >= 0) { values.splice(idx, 1); } else { values.push(v); } document.forms[0].z13.value = values.join(' '); return false; } 
  • possible and easier, try it yourself ... - Yura Ivanov
  • brilliant! thanks - sergey
 <html> <head> <title>Untitled</title> <script type="text/javascript"> window.onload = function() { var div = document.getElementById("lnks"); var input = document.getElementById("input"); for(var i = 0; i < div.childNodes.length; i++) { div.childNodes[i].onclick = function() { if(input.value == '') { input.value += this.innerHTML + ','; } else { var arr = input.value.split(','); for(var i = 0; i < arr.length; i++) { if(arr[i] == this.innerHTML) { var stop = true; input.value = input.value.replace(this.innerHTML + ',', ''); } } if(stop != true) { input.value += this.innerHTML + ','; } } } } } </script> </head> <body> <div id="lnks"> <a>01</a> <a>02</a> <a>03</a> <a>07</a> <a>15</a> </div> <input type="text" name="input" id="input" readonly="true" value="" /> </body> </html> 
  • the solution is cool, even writing is simplified. (unfortunately, the "accept" in the subject has already been stuck, but you cannot put 2) - sergey
  • @mixalef is okay! Use =) - Palmervan

when you click on a link, read the current value and add a new one to it

 var val = document.form.z13.value document.form.z13.value = val+' 02' 

and before that there should be a check with a regular statement - is there such a value inside the line, if there is, then instead of adding it, simply erase it.

    <a href="#d" onclick="document.form.z13"> <input type = "button" onclick = "document.form.z14"> </a>

    • do not minus the beginner, it is a pity somehow - makregistr
    • and who is minus? - Samo