0

what i am trying to do is, if text box have some value page should move to center div, when text box is empty page should stay on start position.

in my code page always goes to second div.

FIDDLE

$(document).ready(function() {
  if ((document.getElementById("textbox").value != null)) {

    // Handler for .ready() called.
    $('html, body').animate({
      scrollTop: $('#what').offset().top
    }, 'slow');

  }

});
div {
  height: 900px;
  border: 2px solid #333;
  padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="textbox" />
<div id="mydiv">DATA FOR SAMPLE 1</div>
<div id="what">SAMPLE DATA</div>

2
  • 2
    There is no way a textbox value will be null in modern browsers. In IE4 or so it would be null if the value attribute was not specified on the tag Commented Jul 16, 2020 at 12:17
  • 1
    value is always an empty string if it's not filled in. Even if the attribute is missing. Commented Jul 16, 2020 at 12:18

3 Answers 3

1

Test for the length of the trimmed value

That can handle spaces

NOTE the value of "0" is truthy

$(function() { // on page load
  if ($("#textbox").val().trim().length > 0) { // === 0 when empty
    // Handler for .ready() called.
    $('html, body').animate({
      scrollTop: $('#what').offset().top
    }, 'slow');
  }
});
div {
  height: 900px;
  border: 2px solid #333;
  padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="textbox" value="" />
<div id="mydiv">DATA FOR SAMPLE 1</div>
<div id="what">SAMPLE DATA</div>

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

4 Comments

i want to run it automatically when page loads
My code runs automatically on page load, I only removed unnecessary code
if i put no value in text box, $(function() { if ($("#textbox").val().trim().length < 0) { // Handler for .ready() called. alert("dd"); } }); why this isn't working
because it is === 0 when empty
0

Remove the camparison with null:

if (document.getElementById("textbox").value !== '') {

    // Handler for .ready() called.
    $('html, body').animate({
      scrollTop: $('#what').offset().top
    }, 'slow');

  }

3 Comments

@mplungjan, I didn't knew "0" is false as well. I thought integer 0 is false.
"0" is indeed truthy. Also see the JS equality table - go to the if () tab.
This is actually ok as long as there are no spaces in the field
0

Change != null to !==''

$(document).ready(function() {
  if ((document.getElementById("textbox").value !== '')) {

    // Handler for .ready() called.
    $('html, body').animate({
      scrollTop: $('#what').offset().top
    }, 'slow');

  }

});
div {
  height: 900px;
  border: 2px solid #333;
  padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="textbox" />
<div id="mydiv">DATA FOR SAMPLE 1</div>
<div id="what">SAMPLE DATA</div>

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.