0

So, I have the following code, and it's not working, and I'm not sure why. Any suggestions?

HTML:

<input type="number" placeholder="#" id="lineSpace"/>

<br/><br/><button id="go">update text!</button>

JavaScript (jQuery)

$(document).ready(function() {
var lineSpace = $("#lineSpace").val();

$("#go").click(function(){
alert("LINE SPACE: "+lineSpace+"");
});

This was a test to see if my variable was being gathered, but it seems not. The alert appears but simply says "LINE SPACE: ", without placing the variable after it. When I define the like this var lineSpace = "random number";(which is no use to me, as I need a value that is entered from the user-side), however, it works fine.

1 Answer 1

1

Because you are assigning lineSpace when the page loads, and it's obviously blank then.

Move var lineSpace = $("#lineSpace").val(); into your click handler (above the alert() function call).

Sign up to request clarification or add additional context in comments.

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.