How to pass a function as a new variable?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
10 views
Posted a simplified version of the script, I can not understand in what business
The script generates a random number, a global variable.
On the page there is a input to enter a number and click the button to generate a new.
The bottom line is this, when you generate the new numbers, the check function is a new number not recorded, is there any option to have the function handled already new value of the variable? Or, to duplicate the function in the action on click, is there a way to allow functions with the previous variable?
https://jsfiddle.net/ekwb2k6j/1/

body { padding-top: 100px; text-align: center; } input { font-size: 24px; } p { text-align: center; font-size: 30px; } .green { color: green; }

getRandomInRange function(min, max) { // a random number in a specified interval return Math.floor(Math.random() * (max - min + 1)) + min; } function randomA() { // a = getRandomInRange(1, 10); $('#a').text(a); // output and } function chek(x, y) { // x is the id of the input, y is the variable $(x).change(function(){ var value = $(this).val(); console.log(y); if (value != y) { $(x).addClass("green"); } else { $(x).removeClass("green"); $(x).animate({width: 'toggle'}, 500); }; }) } function gen() { // generate a new variable $('#gen').on("click", function() { $('#input').fadeIn(0); randomA(); $('input:text').val("); }); } $(document).ready(function(){ $('input:text').val("); randomA(); chek('#input', a); gen(); });
by | 10 views

1 Answer

0 like 0 dislike
Corrected, only to work - https://jsfiddle.net/ekwb2k6j/2/
var a; getRandomInRange function(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function randomA() { // a = getRandomInRange(6, 9); $('#a').text(a); // output and } function chek(x, y) { // x is the id of the input, y is the variable var value = $(x).val(); if (value != y) { $(x).addClass("green"); } else { $(x).removeClass("green"); $(x).animate({width: 'toggle'}, 500); } } $('#input').change(function(){ chek(this, a) }) $('#gen').on("click", function() { $('input:text').val("); $('#input').fadeIn(0); randomA(); }); $(document).ready(function(){ $('input:text').val("); randomA(); });

Generally it is not necessary to write...
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
35,573 users