snipt

Ctrl+h for KB shortcuts

JavaScript

Sync Textbox Input 1 with Input 2 using Same Value - Javascript + Html

<script>
function sync()
{
  var n1 = document.getElementById('n1');
  var n2 = document.getElementById('n2');
  n2.value = n1.value;
}
</script>


<input type="text" name="n1" id="n1" onkeyup="sync()">
<input type="text" name="n2" id="n2"/>

Description

A simple script to sync input typing between two fields at the same time.
https://snipt.net/embed/9cf89e3cdef9ab526e8af336eb924cb0/
/raw/9cf89e3cdef9ab526e8af336eb924cb0/
9cf89e3cdef9ab526e8af336eb924cb0
js
JavaScript
15
2019-08-25T09:11:10
True
False
False
Mar 23, 2016 at 12:28 PM
/api/public/snipt/144906/
sync-textbox-input-1-with-input-2-using-same-value-javascript-html
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="o">&lt;</span><span class="nx">script</span><span class="o">&gt;</span> </span><span id="L-2"><a name="L-2"></a><span class="kd">function</span> <span class="nx">sync</span><span class="p">()</span> </span><span id="L-3"><a name="L-3"></a><span class="p">{</span> </span><span id="L-4"><a name="L-4"></a> <span class="kd">var</span> <span class="nx">n1</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">&#39;n1&#39;</span><span class="p">);</span> </span><span id="L-5"><a name="L-5"></a> <span class="kd">var</span> <span class="nx">n2</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">&#39;n2&#39;</span><span class="p">);</span> </span><span id="L-6"><a name="L-6"></a> <span class="nx">n2</span><span class="p">.</span><span class="nx">value</span> <span class="o">=</span> <span class="nx">n1</span><span class="p">.</span><span class="nx">value</span><span class="p">;</span> </span><span id="L-7"><a name="L-7"></a><span class="p">}</span> </span><span id="L-8"><a name="L-8"></a><span class="o">&lt;</span><span class="err">/script&gt;</span> </span><span id="L-9"><a name="L-9"></a> </span><span id="L-10"><a name="L-10"></a> </span><span id="L-11"><a name="L-11"></a><span class="o">&lt;</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">&quot;text&quot;</span> <span class="nx">name</span><span class="o">=</span><span class="s2">&quot;n1&quot;</span> <span class="nx">id</span><span class="o">=</span><span class="s2">&quot;n1&quot;</span> <span class="nx">onkeyup</span><span class="o">=</span><span class="s2">&quot;sync()&quot;</span><span class="o">&gt;</span> </span><span id="L-12"><a name="L-12"></a><span class="o">&lt;</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">&quot;text&quot;</span> <span class="nx">name</span><span class="o">=</span><span class="s2">&quot;n2&quot;</span> <span class="nx">id</span><span class="o">=</span><span class="s2">&quot;n2&quot;</span><span class="o">/&gt;</span> </span></pre></div> </td></tr></table>
"sync values", "text sync", html, javascript, js, sync
--- 
+++ 
@@ -0,0 +1,14 @@
+<script>
+function sync()
+{
+  var n1 = document.getElementById('n1');
+  var n2 = document.getElementById('n2');
+  n2.value = n1.value;
+}
+</script>
+
+
+<input type="text" name="n1" id="n1" onkeyup="sync()">
+<input type="text" name="n2" id="n2"/>
+
+
  • KadeJM
  • 3 years, 5 months ago