/    Sign up×
Community /Pin to ProfileBookmark

Javascript: changing position of elements of array using function

Hello

I would like to change position of elements of the array I created within the “shift” function below. When I fill the input boxes (document.getElementById(“input_06_07_08_09”).value) and then click “shift” button, the elements of array in the output boxes (document.getElementById(“input_10_11_12_13”).value) shift just for one time.

When I click “shift” button again and again, there is no change in the output boxes. The elements do not shift anymore.

I want input boxes not to change when I fill them and click “shift” button, and output boxes change (shift elements) every time I click the “shift” button.

Could you please give your suggestions to correct the code below? How can I use global variables to solve the problem?

Best regards,

`
<script>

function shift() {

var op06 = document.getElementById(“input_06”).value;
var op07 = document.getElementById(“input_07”).value;
var op08 = document.getElementById(“input_08”).value;
var op09 = document.getElementById(“input_09”).value;

var array = [op06, op07, op08, op09];

var i = 3;
t = array[3];
for (i = 3 ; i >= 0; i–) {
array[i] = array[i-1];
}
array[0] = t;

document.getElementById(“input_10”).value = array[0];
document.getElementById(“input_11”).value = array[1];
document.getElementById(“input_12”).value = array[2];
document.getElementById(“input_13”).value = array[3];
}

function clear ()
{
document.getElementById(“input_10”).value = “”;
document.getElementById(“input_11”).value = “”;
document.getElementById(“input_12”).value = “”;
document.getElementById(“input_13”).value = “”;
}

</script>

<div class=”container-fluid”>
<h1><center>Shift</h1>
<div class=”row”>
<div class=”col-md-4″>
<div class=”border”>
<div id=”op06″><center><input id=”input_06″ type=”text” class=”inputs” ></div>
<div id=”op07″><center><input id=”input_07″ type=”text” class=”inputs” ></div>
<div id=”op08″><center><input id=”input_08″ type=”text” class=”inputs” ></div>
<div id=”op09″><center><input id=”input_09″ type=”text” class=”inputs” ></div>
</div>
</div>
<div class=”col-md-4″>
<div class=”next-div”>
<button type=”button” class=”btn btn-success btn-block btn-md” onclick=”shift()”>Shift</button>
<button type=”button” class=”btn btn-success btn-block btn-md” onclick=”clear()”>Clear</button>
</div>
</div>
<div class=”col-md-4″>
<div class=”border”>
<div id=”op10″><center><input id=”input_10″ type=”text” class=”inputs” readonly=”true”></div>
<div id=”op11″><center><input id=”input_11″ type=”text” class=”inputs” readonly=”true”></div>
<div id=”op12″><center><input id=”input_12″ type=”text” class=”inputs” readonly=”true”></div>
<div id=”op13″><center><input id=”input_13″ type=”text” class=”inputs” readonly=”true”></div>
</div>
</div>
</div>
</div>

`

to post a comment
Full-stack DeveloperJavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJun 26.2020 — This is the key for finding the reason and fixing the error:
I want input boxes not to change when I fill them and click "shift" button, and output boxes change (shift elements) every time I click the "shift" button.[/quote]The source values for your script are the input values always and thus the result is always the same: inputs shifted by one position. Fix: Copy the input to the outputs in the beginning and then shift the outputs only.
Copy linkTweet thisAlerts:
@ltaylanozgurauthorJun 26.2020 — @Sempervivum#1619953 actually, the input and output boxes should be different. Thanks.
Copy linkTweet thisAlerts:
@SempervivumJun 26.2020 — @ltaylanozgur#1619954 They will be, after shifting the first time. Besides this, it would be better to make the array global and the values into it once at startup. Then, when shift is clicked, shift the array and copy the values to the outputs.
Copy linkTweet thisAlerts:
@SempervivumJun 26.2020 — Check if this does what you require:
<i>
</i> &lt;div class="container-fluid"&gt;
&lt;h1&gt;
&lt;center&gt;Shift
&lt;/h1&gt;
&lt;div class="row"&gt;
&lt;div class="col-md-4"&gt;
&lt;div class="border"&gt;
&lt;div id="op06"&gt;
&lt;center&gt;&lt;input id="input_06" type="text" class="inputs"&gt;
&lt;/div&gt;
&lt;div id="op07"&gt;
&lt;center&gt;&lt;input id="input_07" type="text" class="inputs"&gt;
&lt;/div&gt;
&lt;div id="op08"&gt;
&lt;center&gt;&lt;input id="input_08" type="text" class="inputs"&gt;
&lt;/div&gt;
&lt;div id="op09"&gt;
&lt;center&gt;&lt;input id="input_09" type="text" class="inputs"&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="col-md-4"&gt;
&lt;div class="next-div"&gt;
&lt;button type="button" class="btn btn-success btn-block btn-md" onclick="shift()"&gt;Shift&lt;/button&gt;
&lt;button type="button" class="btn btn-success btn-block btn-md" onclick="clear()"&gt;Clear&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="col-md-4"&gt;
&lt;div class="border"&gt;
&lt;div id="op10"&gt;
&lt;center&gt;&lt;input id="input_10" type="text" class="inputs" readonly="true"&gt;
&lt;/div&gt;
&lt;div id="op11"&gt;
&lt;center&gt;&lt;input id="input_11" type="text" class="inputs" readonly="true"&gt;
&lt;/div&gt;
&lt;div id="op12"&gt;
&lt;center&gt;&lt;input id="input_12" type="text" class="inputs" readonly="true"&gt;
&lt;/div&gt;
&lt;div id="op13"&gt;
&lt;center&gt;&lt;input id="input_13" type="text" class="inputs" readonly="true"&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
var array;
var indexes = [0, 1, 2, 3]
document.querySelector('div.row').addEventListener('input', () =&gt; {
var op06 = document.getElementById("input_06").value;
var op07 = document.getElementById("input_07").value;
var op08 = document.getElementById("input_08").value;
var op09 = document.getElementById("input_09").value;

<i> </i> array = [op06, op07, op08, op09];

<i> </i> });
<i> </i> function shift() {
<i> </i> t = indexes[3];
<i> </i> for (var i = 3; i &gt; 0; i--) {
<i> </i> indexes[i] = indexes[i - 1];
<i> </i> }
<i> </i> indexes[0] = t;

<i> </i> document.getElementById("input_10").value = array[indexes[0]];
<i> </i> document.getElementById("input_11").value = array[indexes[1]];
<i> </i> document.getElementById("input_12").value = array[indexes[2]];
<i> </i> document.getElementById("input_13").value = array[indexes[3]];
<i> </i> }

<i> </i> function clear() {
<i> </i> document.getElementById("input_10").value = "";
<i> </i> document.getElementById("input_11").value = "";
<i> </i> document.getElementById("input_12").value = "";
<i> </i> document.getElementById("input_13").value = "";
<i> </i> }
<i> </i>&lt;/script&gt;


BTW:
  • 1. The <center> tag is outdated and it's not closed in your code

  • 2. You better use code tags instead of backtics when posting code: `your code here`
  • Copy linkTweet thisAlerts:
    @JMRKERJun 29.2020 — @Sempervivum I have a question about your code actions.

    I assume the addEventListener() code is to update the original array contents should they be changed during operations.

    I also see how the shift() function is changing the output display by using the 'indexes' changes.

    What I don't understand is why the clear() function does not seem to be working when the button is clicked.

    I tried putting an alert('got here') into the function, but the event never seems to be triggered (???)
    Copy linkTweet thisAlerts:
    @SempervivumJun 29.2020 — You are absolutely correct, this is an error in the code. It took me some time and I fixed it by renaming the name `clear</C> to <C>clearIt</C>. I suspected that <C>clear` is a reserved word in JS but could not confirm this. The code is not clean as the event listeners are added by addEventListener on one hand and inline in the HTML on the other. This works but is ugly.
    &lt;div class="container-fluid"&gt;
    &lt;h1&gt;
    Shift
    &lt;/h1&gt;
    &lt;div class="row"&gt;
    &lt;div class="col-md-4"&gt;
    &lt;div class="border"&gt;
    &lt;div id="op06"&gt;
    &lt;input id="input_06" type="text" class="inputs"&gt;
    &lt;/div&gt;
    &lt;div id="op07"&gt;
    &lt;input id="input_07" type="text" class="inputs"&gt;
    &lt;/div&gt;
    &lt;div id="op08"&gt;
    &lt;input id="input_08" type="text" class="inputs"&gt;
    &lt;/div&gt;
    &lt;div id="op09"&gt;
    &lt;input id="input_09" type="text" class="inputs"&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-md-4"&gt;
    &lt;div class="next-div"&gt;
    &lt;button type="button" class="btn btn-success btn-block btn-md" onclick="shift()"&gt;Shift&lt;/button&gt;
    &lt;button type="button" class="btn btn-success btn-block btn-md" onclick="clearIt()"&gt;Clear&lt;/button&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-md-4"&gt;
    &lt;div class="border"&gt;
    &lt;div id="op10"&gt;
    &lt;input id="input_10" type="text" class="inputs" readonly="true"&gt;
    &lt;/div&gt;
    &lt;div id="op11"&gt;
    &lt;input id="input_11" type="text" class="inputs" readonly="true"&gt;
    &lt;/div&gt;
    &lt;div id="op12"&gt;
    &lt;input id="input_12" type="text" class="inputs" readonly="true"&gt;
    &lt;/div&gt;
    &lt;div id="op13"&gt;
    &lt;input id="input_13" type="text" class="inputs" readonly="true"&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;script&gt;
    var arr;
    var indexes = [0, 1, 2, 3]
    document.querySelector('div.row').addEventListener('input', () =&gt; {
    var op06 = document.getElementById("input_06").value;
    var op07 = document.getElementById("input_07").value;
    var op08 = document.getElementById("input_08").value;
    var op09 = document.getElementById("input_09").value;

    <i> </i> arr = [op06, op07, op08, op09];

    <i> </i> });
    <i> </i> function shift() {
    <i> </i> var lastIdx = indexes.length - 1;
    <i> </i> var t = indexes[lastIdx];
    <i> </i> for (var i = lastIdx; i &gt; 0; i--) {
    <i> </i> indexes[i] = indexes[i - 1];
    <i> </i> }
    <i> </i> indexes[0] = t;

    <i> </i> document.getElementById("input_10").value = arr[indexes[0]];
    <i> </i> document.getElementById("input_11").value = arr[indexes[1]];
    <i> </i> document.getElementById("input_12").value = arr[indexes[2]];
    <i> </i> document.getElementById("input_13").value = arr[indexes[3]];
    <i> </i> }

    <i> </i> function clearIt() {
    <i> </i> document.getElementById("input_10").value = "";
    <i> </i> document.getElementById("input_11").value = "";
    <i> </i> document.getElementById("input_12").value = "";
    <i> </i> document.getElementById("input_13").value = "";
    <i> </i> arr = [];
    <i> </i> }
    <i> </i>&lt;/script&gt;
    Copy linkTweet thisAlerts:
    @JMRKERJun 29.2020 — While waiting for your reply, I came upon the same solution for the clear() error.

    Then I went crazy and made major modifications to your code so that:
  • 1. You could enter a longer list without using individual ID values for each input and output

  • 2. Optional reverse shift button (just for grins)

  • 3. Shortened JS code for ES6 updates. ONLY tested in FF and Chrome browsers. Not tested on MS browsers.


  • &lt;!DOCTYPE html&gt;&lt;html lang="en"&gt;&lt;head&gt;&lt;title&gt; Test Page &lt;/title&gt;<br/>
    &lt;meta charset="UTF-8"&gt;<br/>
    &lt;meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/&gt;<br/>
    &lt;!-- Modified from: <URL url="https://www.webdeveloper.com/d/390420-javascript-changing-position-of-elements-of-array-using-function/5">https://www.webdeveloper.com/d/390420-javascript-changing-position-of-elements-of-array-using-function/5</URL> --&gt;
    <br/>
    <i> </i><CODE>&lt;!-- link rel="stylesheet" href="common.css" media="screen" --&gt;
    <i> </i>&lt;style&gt;
    <i> </i> .inputs:focus { background-color: lime; }
    <i> </i>&lt;/style&gt;
    <i> </i>&lt;/head&gt;&lt;body&gt;
    <i> </i>
    <i> </i>&lt;div class="container-fluid"&gt;
    <i> </i> &lt;h1&gt; Shift &lt;/h1&gt;
    <i> </i> &lt;div class="row"&gt;
    <i> </i> &lt;div class="col-md-4"&gt;
    <i> </i> &lt;div id='inpGroup' class="border"&gt;
    <i> </i> &lt;div&gt;&lt;input type="text" class="inputs"&gt;&lt;/div&gt;
    <i> </i> &lt;div&gt;&lt;input type="text" class="inputs"&gt;&lt;/div&gt;
    <i> </i> &lt;div&gt;&lt;input type="text" class="inputs"&gt;&lt;/div&gt;
    <i> </i> &lt;div&gt;&lt;input type="text" class="inputs"&gt;&lt;/div&gt;
    <i> </i> &lt;/div&gt;
    <i> </i> &lt;/div&gt;
    <i> </i> &lt;div class="col-md-4"&gt;
    <i> </i> &lt;div class="next-div"&gt;
    <i> </i> &lt;button type="button" class="btn btn-success btn-block btn-md" onclick="shiftEm()"&gt;Shift&lt;/button&gt;
    <i> </i> &lt;button type="button" class="btn btn-success btn-block btn-md" onclick="shiftEm(false)"&gt;Reverse Shift&lt;/button&gt;
    <i> </i> &lt;button type="button" class="btn btn-success btn-block btn-md" onclick="clearEm()"&gt;Clear&lt;/button&gt;
    <i> </i> &lt;/div&gt;
    <i> </i> &lt;/div&gt;
    <i> </i> &lt;div class="col-md-4"&gt;
    <i> </i> &lt;div id="outGroup" class="border"&gt;
    <i> </i> &lt;div&gt;&lt;input type="text" class="inputs" readonly="true"&gt;&lt;/div&gt;
    <i> </i> &lt;div&gt;&lt;input type="text" class="inputs" readonly="true"&gt;&lt;/div&gt;
    <i> </i> &lt;div&gt;&lt;input type="text" class="inputs" readonly="true"&gt;&lt;/div&gt;
    <i> </i> &lt;div&gt;&lt;input type="text" class="inputs" readonly="true"&gt;&lt;/div&gt;
    <i> </i> &lt;/div&gt;
    <i> </i> &lt;/div&gt;
    <i> </i> &lt;/div&gt;
    <i> </i>&lt;/div&gt;
    <i> </i>
    <i> </i>&lt;script&gt;
    <i> </i>console.clear(); // console.log only needed for testing purposes
    <i> </i>
    <i> </i>var array = [];
    <i> </i>const d = document;
    <i> </i>
    <i> </i>d.querySelector('#inpGroup').addEventListener('input', () =&gt; {
    <i> </i> let tarr = [];
    <i> </i> const sel = d.querySelectorAll('#inpGroup input'); // collect pointers
    <i> </i> for (let i=0; i&lt;sel.length; i++) { tarr.push(sel[i].value); } // save pointer values
    <i> </i> array = [...tarr];
    <i> </i>// console.log('Initial list: t',array.join(', '));
    <i> </i>});
    <i> </i>
    <i> </i>function shiftEm( flag = true ) {
    <i> </i> const sel = d.querySelectorAll('#outGroup input');
    <i> </i> sel.forEach( (el,i) =&gt; { el.value = array[(i+1) % sel.length]; } );
    <i> </i> if (flag) { array.push(array.shift()) } // rotate array contents
    <i> </i> else { array.unshift(array.pop()) }; // reverse rotate array contents
    <i> </i>// console.log('shift()', array.join(', '));
    <i> </i>}
    <i> </i>
    <i> </i>function clearEm() {
    <i> </i> const sel = d.querySelectorAll('#outGroup input');
    <i> </i> sel.forEach( el =&gt; { el.value = ''; });
    <i> </i>}
    <i> </i>
    <i> </i>&lt;/script&gt;
    <i> </i>&lt;/body&gt;&lt;/html&gt;
    <i> </i>

    **Modified only because I'm bored and stuck at home during this COVID-19 quarantine.**

    ×

    Success!

    Help @ltaylanozgur spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.26,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...