I need to randomize the number part of string on every click, hence 1 after sample should be randomized (sample1). I am concerned with the data-sample part, but showing the value as well to show the same click don’t do any good. I suspect the number is just string on the following clicks.
JS fiddle sample.
HTML:
<input id="input" value="sample1" data-sample="sample1"/>
<button id="button">Randomize</button>
JQUERY:
var newRandomNumber = (Math.random()*10)+1;
$("#button").click(function () {
var value = Math.floor(newRandomNumber);
$("#input").attr('data-sample', '').attr('data-sample', 'sample' + value).val('sample' + value);
return false;
});
Any hint is very much appreciated. Thanks.
Move the actual random number generation inside the click event. jsFiddle example