Em Ji Madhu Em Ji Madhu - 1 month ago 9
CSS Question

How to Get user input in jQuery and use in the same?

I want to get user input and process that input in same jQuery. How can I do that?

Say for example, I have a ball bouncing animation in jQuery, I want to ask the user how many times that ball has to bounce and based on his/her input, the ball has to bounce. How can I accomplish that? I know how to get values, but I don't know how to process the value.

For getting value I use:

$("button:#Get").click(function () {

$('input:number').val();

});


here is an example im trying to use the function,



$(function() {

var time = 500;
var bounces = 20;
var top_bounce = 10;

function bounceDown(){
$("#ball").animate({left:10, top: bounces*10}, time, function(){
bounceUp();
});
};



function bounceUp() {
$("#ball").animate({top: top_bounce}, time);
top_bounce = top_bounce + 10;
};

function shadowUp(){
$("#shadow").animate({width: 100, height: 5, left: 10, top: bounces*15, opacity: 1}, time,
function(){
shadowDown();
});
};

function shadowDown() {
$("#shadow").animate({width: 0, height: 0, left: 15, top: bounces*15, opacity: 0}, time);
};

function finalDown(){
$("#ball").animate({left:10, top: bounces*10}, time);
};

function finalShadow(){
$("#shadow").animate({width: 100, height: 5, left: 10, top: bounces*15, opacity: 1}, time);
};

$('#Get').click(function () {
for (var i = 0; i < bounces; i++){
setTimeout(function(){
bounceDown();
shadowUp();
}, time*2*i);
setTimeout(function(){
finalDown();
finalShadow();
}, bounces*1000);
};
});



});

body {
background-color: black;
position: absolute;
width: 100%;
height: 100%;
overflow: hidden;
}

#container {
position: absolute;
left: 50%;
width: 500px;
height: 600px;
}

#ball {
width: 100px;
height: 100px;
position: absolute;
background-color: #e65454;
border-radius: 50%;
}

#shadow {
position: absolute;
height: 5px;
width: 100px;
bottom:0;
background: radial-gradient(ellipse at center, rgba(91,91,91,1) 0%,rgba(142,142,142,0.84) 16%,rgba(227,228,229,0) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#5b5b5b', endColorstr='#00e3e4e5',GradientType=1 );
opacity:0;
}

div.inp{
position:fixed;
bottom:35px;
left:30%;
width:100%;
}

input[type=number] {
width: 200px;
padding: 12px 10px;
margin: 0;
box-sizing: border-box;
border: 2px solid #e65454;
border-radius: 8px;
outline:0;
}

button {
background-color: transparent;
color: white;
padding: 16px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
transition-duration: 0.4s;
cursor: pointer;
outline:0;
border: 2px solid #e65454;
}

button:hover {
background-color: #e65454;
color: black;
border: 2px solid transparent;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script>
<div class="inp">
<input type="number" value="How Many Bounces?"></input>
<button id="Get">Set</button>
<button id="Reset">Reset</button>
</div>
<div id="container">
<div id="ball"></div>
<div id="shadow"></div>
</div>





now i want ask user for the value and based on the value, "bounces" should updated in jquery.

Answer

Define a function instead of anonymous function.

$(function() {
    //function body
}

define like

function bounce(){
    //function body
}

you can also pass the variable that determines the no. of bounces of ball, 'bounces' in your case.

function bounce(var bounces){
    //function body
    //write everything that you have written in your function except var bounces
    //as it is taken from input
}

Then you can call the above function in your onClick method. Also as said by @Franco you are using incorrect selector. so you can write something like

<input type="number" id="number" value="How Many Bounces?"></input>

Following will be your final js

$("#Get").click(function () {
   bounces = $('#number').val();
   bounce(bounces);
 });

function bounce(bounces) {
    
    var time = 500;
    var top_bounce = 10;
  
 
    
    function bounceDown(){
      $("#ball").animate({left:10, top: bounces*10}, time, function(){
        bounceUp();
      });
    };
    
    
    
    function bounceUp() {
      $("#ball").animate({top: top_bounce}, time);
      top_bounce = top_bounce + 10;
    };
    
    function shadowUp(){
      $("#shadow").animate({width: 100, height: 5, left: 10, top: bounces*15, opacity: 1}, time,    
    function(){
        shadowDown();
      });
    };
    
    function shadowDown() {
      $("#shadow").animate({width: 0, height: 0, left: 15, top: bounces*15, opacity: 0}, time);
    };     
    
    function finalDown(){
        $("#ball").animate({left:10, top: bounces*10}, time);
    };
    
    function finalShadow(){
    $("#shadow").animate({width: 100, height: 5, left: 10, top: bounces*15, opacity: 1}, time);    
    };
    
    
      for (var i = 0; i < bounces; i++){
        setTimeout(function(){
          bounceDown();
          shadowUp();      
        }, time*2*i);
        setTimeout(function(){
          finalDown();
          finalShadow();
      }, bounces*1000);
      };               
    
    
    
    
};

Hope this Helps.