-1

I am working on project where I want to change placeholder each time when user will refresh page. I want only 1st to 9th position in placeholder.

<div class="control-group">
    <div class="controls">
        <div class="main_input_box">
            <label >PIN </label>
            <input class="pass" type="password" placeholder="5th" />
            <input class="pass" type="password" placeholder="2nd" />
            <input class="pass" type="password" placeholder="1st" />
        </div>
    </div>
</div>

When user will load page all placeholder's value should be changed, similar to the rand() function in PHP. How can this be done in jQuery?

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
amit sutar
  • 115
  • 1
  • 3
  • 15
  • 1
    Please show the attempt you've made to solve this yourself. Also, how can you number 3 inputs `1st` to `9th`...? – Rory McCrossan Oct 16 '17 at 07:32
  • i am not done anything yet. i cant understand it. this work is for user login.If user abc's password is 123456789 then input field will be like get 1st,2nd and 4th position from password. if user will reload page then it will ask for 3rd,6th,5th position from password etc. – amit sutar Oct 16 '17 at 07:34
  • To do it with jQuery you just need to [iterate all 3 inputs](http://api.jquery.com/jquery.each/), [generate a random number](https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range) between your needed range and check this random number isn't repeated. – Jordi Nebot Oct 16 '17 at 07:46

1 Answers1

1

I hope it would help you:

var temp;
var nums = [];
$(document).ready(function () {
    $('.pass').each(function(i, obj) {
        do {
            temp = Math.floor(Math.random() * 9) + 1;

            $(this).attr("placeholder", temp);
        } while (nums.indexOf(temp) != -1);
        nums.push(temp);
    });
});

9 and 1 in max and min