1

I have written a small function in javascript to get user input from a text input form to be used as the delay time for fading away but it fails please help

<input type="text" class="form-control" id="new" placeholder="Fade Duration(milliseconds)"/>

var x = document.getElementById("new").value;
$(".fadeInbox").click(function () {
    $("#section1").delay(x).fadeIn();
    $("#section2").delay(x).fadeIn();
});



$(".fadeOutbox").click(function () {
    $("#section1").delay(x).fadeOut();
    $("#section2").delay(x).fadeOut();

});
6
  • change var x = document.getElementById("new").value; to var x = Number(document.getElementById("new").value); Commented Nov 19, 2015 at 11:51
  • The value property of an HTMLInputElement returns a string Commented Nov 19, 2015 at 11:53
  • thanks i will try that Commented Nov 19, 2015 at 11:55
  • if you are using jQuery, it would be easier to get the value with var x = $("#new").val();, writing much less Commented Nov 19, 2015 at 13:07
  • Does any of the answers solve your problem? If so, please select the right answer. Commented Nov 25, 2015 at 9:18

3 Answers 3

1

var x from input is a string not a int, so

var x = parseInt(document.getElementById("new").value);
...

or

var x = +document.getElementById("new").value;
...
Sign up to request clarification or add additional context in comments.

3 Comments

i moved var x = document.getElementById("new").value; into the two functions and solved my problem
Even then you should make them int
will definately do that
0

You should put the

var x = document.getElementById("new").value;

inside the functions, or it will be executed only once immediately after the script loads (and not work).

Comments

0

The delay should be checked inside the functions, otherwise the value will be checked only once - when the page loads. If it's inside the functions it will be checked every time the function is triggered.

$(".fadeInbox").click(function () {
    var x = document.getElementById("new").value;
    $("#section1").delay(x).fadeIn();
    $("#section2").delay(x).fadeIn();
});

$(".fadeOutbox").click(function () {
    var x = document.getElementById("new").value;
    $("#section1").delay(x).fadeOut();
    $("#section2").delay(x).fadeOut();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.