jQuery width function parameters not working










1















I'm using a code found from a post in stackoverflow to scroll a division horizontally. I can scroll the div for full width but can't scroll for a specific amount of width.






$("div").on("click", function(event) 
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width()
, "slow");
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>





Above code is working!



jQuery documentation says that width() function can get values as parameters but when I try like follows, the scrolling is not working.



$("div").on("click", function(event) 
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width(50)
, "slow");
);


How can I solve this?










share|improve this question
























  • you cannot use like that. you should get width and then add ur value.. like .width() + 100 because .width() function returns the width

    – Manjunath
    Oct 30 '18 at 13:23







  • 1





    Just remove the $(document).width() entirely. eg. scrollLeft: 50

    – Turnip
    Oct 30 '18 at 13:24











  • And, you can remove event.preventDefault() since there is no default behavior from clicking a div.

    – Scott Marcus
    Oct 30 '18 at 13:25











  • The documentation doesn't say you can get values with parameters. It says that you can set values by passing parameters. You don't want to set the document.width() here, you just want to scroll by an amount, so simply pass that amount without any mention of width().

    – Scott Marcus
    Oct 30 '18 at 13:27















1















I'm using a code found from a post in stackoverflow to scroll a division horizontally. I can scroll the div for full width but can't scroll for a specific amount of width.






$("div").on("click", function(event) 
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width()
, "slow");
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>





Above code is working!



jQuery documentation says that width() function can get values as parameters but when I try like follows, the scrolling is not working.



$("div").on("click", function(event) 
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width(50)
, "slow");
);


How can I solve this?










share|improve this question
























  • you cannot use like that. you should get width and then add ur value.. like .width() + 100 because .width() function returns the width

    – Manjunath
    Oct 30 '18 at 13:23







  • 1





    Just remove the $(document).width() entirely. eg. scrollLeft: 50

    – Turnip
    Oct 30 '18 at 13:24











  • And, you can remove event.preventDefault() since there is no default behavior from clicking a div.

    – Scott Marcus
    Oct 30 '18 at 13:25











  • The documentation doesn't say you can get values with parameters. It says that you can set values by passing parameters. You don't want to set the document.width() here, you just want to scroll by an amount, so simply pass that amount without any mention of width().

    – Scott Marcus
    Oct 30 '18 at 13:27













1












1








1








I'm using a code found from a post in stackoverflow to scroll a division horizontally. I can scroll the div for full width but can't scroll for a specific amount of width.






$("div").on("click", function(event) 
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width()
, "slow");
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>





Above code is working!



jQuery documentation says that width() function can get values as parameters but when I try like follows, the scrolling is not working.



$("div").on("click", function(event) 
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width(50)
, "slow");
);


How can I solve this?










share|improve this question
















I'm using a code found from a post in stackoverflow to scroll a division horizontally. I can scroll the div for full width but can't scroll for a specific amount of width.






$("div").on("click", function(event) 
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width()
, "slow");
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>





Above code is working!



jQuery documentation says that width() function can get values as parameters but when I try like follows, the scrolling is not working.



$("div").on("click", function(event) 
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width(50)
, "slow");
);


How can I solve this?






$("div").on("click", function(event) 
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width()
, "slow");
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>





$("div").on("click", function(event) 
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width()
, "slow");
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>






javascript jquery html css






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:30









Shiladitya

9,44692031




9,44692031










asked Oct 30 '18 at 13:21







user10540027



















  • you cannot use like that. you should get width and then add ur value.. like .width() + 100 because .width() function returns the width

    – Manjunath
    Oct 30 '18 at 13:23







  • 1





    Just remove the $(document).width() entirely. eg. scrollLeft: 50

    – Turnip
    Oct 30 '18 at 13:24











  • And, you can remove event.preventDefault() since there is no default behavior from clicking a div.

    – Scott Marcus
    Oct 30 '18 at 13:25











  • The documentation doesn't say you can get values with parameters. It says that you can set values by passing parameters. You don't want to set the document.width() here, you just want to scroll by an amount, so simply pass that amount without any mention of width().

    – Scott Marcus
    Oct 30 '18 at 13:27

















  • you cannot use like that. you should get width and then add ur value.. like .width() + 100 because .width() function returns the width

    – Manjunath
    Oct 30 '18 at 13:23







  • 1





    Just remove the $(document).width() entirely. eg. scrollLeft: 50

    – Turnip
    Oct 30 '18 at 13:24











  • And, you can remove event.preventDefault() since there is no default behavior from clicking a div.

    – Scott Marcus
    Oct 30 '18 at 13:25











  • The documentation doesn't say you can get values with parameters. It says that you can set values by passing parameters. You don't want to set the document.width() here, you just want to scroll by an amount, so simply pass that amount without any mention of width().

    – Scott Marcus
    Oct 30 '18 at 13:27
















you cannot use like that. you should get width and then add ur value.. like .width() + 100 because .width() function returns the width

– Manjunath
Oct 30 '18 at 13:23






you cannot use like that. you should get width and then add ur value.. like .width() + 100 because .width() function returns the width

– Manjunath
Oct 30 '18 at 13:23





1




1





Just remove the $(document).width() entirely. eg. scrollLeft: 50

– Turnip
Oct 30 '18 at 13:24





Just remove the $(document).width() entirely. eg. scrollLeft: 50

– Turnip
Oct 30 '18 at 13:24













And, you can remove event.preventDefault() since there is no default behavior from clicking a div.

– Scott Marcus
Oct 30 '18 at 13:25





And, you can remove event.preventDefault() since there is no default behavior from clicking a div.

– Scott Marcus
Oct 30 '18 at 13:25













The documentation doesn't say you can get values with parameters. It says that you can set values by passing parameters. You don't want to set the document.width() here, you just want to scroll by an amount, so simply pass that amount without any mention of width().

– Scott Marcus
Oct 30 '18 at 13:27





The documentation doesn't say you can get values with parameters. It says that you can set values by passing parameters. You don't want to set the document.width() here, you just want to scroll by an amount, so simply pass that amount without any mention of width().

– Scott Marcus
Oct 30 '18 at 13:27












4 Answers
4






active

oldest

votes


















0














$(document).width() function provide the window width. That means $(document).width() is equal to a value. Then you have to replace $(document).width() with your value. That's how it work.



$("div").on("click", function(event) 
event.preventDefault();
$("html, body").animate(
scrollLeft: "100px"
, "slow");
);





share|improve this answer






























    0














    Here you go with a solution



    Documentation: https://api.jquery.com/scrollleft/






    $("div").on("click", function(event) 
    event.preventDefault();
    $("html, body").animate(
    scrollLeft: "50px"
    , "slow");
    );

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>





    Hope this will help you.






    share|improve this answer






























      0














      That's because the construction $(document).width(50) is not valid, you can just use a regular number, or the result of calculation instead. For example, these results are valid:



      $(document).width()
      50
      $(document).width() - 50


      Here is an example:






      $("div").on("click", function(event) 
      event.preventDefault();
      $("html, body").animate(
      scrollLeft: $(document).width() - 1000
      , "slow");
      );

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div>
      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
      </div>








      share|improve this answer
































        0














        Reason



        Reason why scrollLeft: $(document).width(50) didn't work because $(document).width(50) is not returning a valid number. (Since it is a setter method.)



        Fixes




        .scrollLeft( value ) Is expecting value of type: Number (An integer indicating the new position to set the scroll bar to.)




        $("div").on("click", function(event) 
        event.preventDefault();
        var ScrollValue = $(document).width() - 100;
        $("html, body").animate(
        scrollLeft: ScrollValue
        , "slow");
        );


        ScrollValue can be calculated accordingly to your requirement and be passed as an integer value.



        Hope this solves your problem, For more info., refer: https://api.jquery.com/scrollleft/






        share|improve this answer






















          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53065263%2fjquery-width-function-parameters-not-working%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown
























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          $(document).width() function provide the window width. That means $(document).width() is equal to a value. Then you have to replace $(document).width() with your value. That's how it work.



          $("div").on("click", function(event) 
          event.preventDefault();
          $("html, body").animate(
          scrollLeft: "100px"
          , "slow");
          );





          share|improve this answer



























            0














            $(document).width() function provide the window width. That means $(document).width() is equal to a value. Then you have to replace $(document).width() with your value. That's how it work.



            $("div").on("click", function(event) 
            event.preventDefault();
            $("html, body").animate(
            scrollLeft: "100px"
            , "slow");
            );





            share|improve this answer

























              0












              0








              0







              $(document).width() function provide the window width. That means $(document).width() is equal to a value. Then you have to replace $(document).width() with your value. That's how it work.



              $("div").on("click", function(event) 
              event.preventDefault();
              $("html, body").animate(
              scrollLeft: "100px"
              , "slow");
              );





              share|improve this answer













              $(document).width() function provide the window width. That means $(document).width() is equal to a value. Then you have to replace $(document).width() with your value. That's how it work.



              $("div").on("click", function(event) 
              event.preventDefault();
              $("html, body").animate(
              scrollLeft: "100px"
              , "slow");
              );






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 9 '18 at 11:49









              Dean JohnsDean Johns

              1




              1























                  0














                  Here you go with a solution



                  Documentation: https://api.jquery.com/scrollleft/






                  $("div").on("click", function(event) 
                  event.preventDefault();
                  $("html, body").animate(
                  scrollLeft: "50px"
                  , "slow");
                  );

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>





                  Hope this will help you.






                  share|improve this answer



























                    0














                    Here you go with a solution



                    Documentation: https://api.jquery.com/scrollleft/






                    $("div").on("click", function(event) 
                    event.preventDefault();
                    $("html, body").animate(
                    scrollLeft: "50px"
                    , "slow");
                    );

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>





                    Hope this will help you.






                    share|improve this answer

























                      0












                      0








                      0







                      Here you go with a solution



                      Documentation: https://api.jquery.com/scrollleft/






                      $("div").on("click", function(event) 
                      event.preventDefault();
                      $("html, body").animate(
                      scrollLeft: "50px"
                      , "slow");
                      );

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>





                      Hope this will help you.






                      share|improve this answer













                      Here you go with a solution



                      Documentation: https://api.jquery.com/scrollleft/






                      $("div").on("click", function(event) 
                      event.preventDefault();
                      $("html, body").animate(
                      scrollLeft: "50px"
                      , "slow");
                      );

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>





                      Hope this will help you.






                      $("div").on("click", function(event) 
                      event.preventDefault();
                      $("html, body").animate(
                      scrollLeft: "50px"
                      , "slow");
                      );

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>





                      $("div").on("click", function(event) 
                      event.preventDefault();
                      $("html, body").animate(
                      scrollLeft: "50px"
                      , "slow");
                      );

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Oct 30 '18 at 13:25









                      ShiladityaShiladitya

                      9,44692031




                      9,44692031





















                          0














                          That's because the construction $(document).width(50) is not valid, you can just use a regular number, or the result of calculation instead. For example, these results are valid:



                          $(document).width()
                          50
                          $(document).width() - 50


                          Here is an example:






                          $("div").on("click", function(event) 
                          event.preventDefault();
                          $("html, body").animate(
                          scrollLeft: $(document).width() - 1000
                          , "slow");
                          );

                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                          <div>
                          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
                          </div>








                          share|improve this answer





























                            0














                            That's because the construction $(document).width(50) is not valid, you can just use a regular number, or the result of calculation instead. For example, these results are valid:



                            $(document).width()
                            50
                            $(document).width() - 50


                            Here is an example:






                            $("div").on("click", function(event) 
                            event.preventDefault();
                            $("html, body").animate(
                            scrollLeft: $(document).width() - 1000
                            , "slow");
                            );

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                            <div>
                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
                            </div>








                            share|improve this answer



























                              0












                              0








                              0







                              That's because the construction $(document).width(50) is not valid, you can just use a regular number, or the result of calculation instead. For example, these results are valid:



                              $(document).width()
                              50
                              $(document).width() - 50


                              Here is an example:






                              $("div").on("click", function(event) 
                              event.preventDefault();
                              $("html, body").animate(
                              scrollLeft: $(document).width() - 1000
                              , "slow");
                              );

                              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                              <div>
                              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
                              </div>








                              share|improve this answer















                              That's because the construction $(document).width(50) is not valid, you can just use a regular number, or the result of calculation instead. For example, these results are valid:



                              $(document).width()
                              50
                              $(document).width() - 50


                              Here is an example:






                              $("div").on("click", function(event) 
                              event.preventDefault();
                              $("html, body").animate(
                              scrollLeft: $(document).width() - 1000
                              , "slow");
                              );

                              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                              <div>
                              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
                              </div>








                              $("div").on("click", function(event) 
                              event.preventDefault();
                              $("html, body").animate(
                              scrollLeft: $(document).width() - 1000
                              , "slow");
                              );

                              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                              <div>
                              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
                              </div>





                              $("div").on("click", function(event) 
                              event.preventDefault();
                              $("html, body").animate(
                              scrollLeft: $(document).width() - 1000
                              , "slow");
                              );

                              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                              <div>
                              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
                              </div>






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Oct 30 '18 at 13:44

























                              answered Oct 30 '18 at 13:26









                              Commercial SuicideCommercial Suicide

                              9,981103354




                              9,981103354





















                                  0














                                  Reason



                                  Reason why scrollLeft: $(document).width(50) didn't work because $(document).width(50) is not returning a valid number. (Since it is a setter method.)



                                  Fixes




                                  .scrollLeft( value ) Is expecting value of type: Number (An integer indicating the new position to set the scroll bar to.)




                                  $("div").on("click", function(event) 
                                  event.preventDefault();
                                  var ScrollValue = $(document).width() - 100;
                                  $("html, body").animate(
                                  scrollLeft: ScrollValue
                                  , "slow");
                                  );


                                  ScrollValue can be calculated accordingly to your requirement and be passed as an integer value.



                                  Hope this solves your problem, For more info., refer: https://api.jquery.com/scrollleft/






                                  share|improve this answer



























                                    0














                                    Reason



                                    Reason why scrollLeft: $(document).width(50) didn't work because $(document).width(50) is not returning a valid number. (Since it is a setter method.)



                                    Fixes




                                    .scrollLeft( value ) Is expecting value of type: Number (An integer indicating the new position to set the scroll bar to.)




                                    $("div").on("click", function(event) 
                                    event.preventDefault();
                                    var ScrollValue = $(document).width() - 100;
                                    $("html, body").animate(
                                    scrollLeft: ScrollValue
                                    , "slow");
                                    );


                                    ScrollValue can be calculated accordingly to your requirement and be passed as an integer value.



                                    Hope this solves your problem, For more info., refer: https://api.jquery.com/scrollleft/






                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      Reason



                                      Reason why scrollLeft: $(document).width(50) didn't work because $(document).width(50) is not returning a valid number. (Since it is a setter method.)



                                      Fixes




                                      .scrollLeft( value ) Is expecting value of type: Number (An integer indicating the new position to set the scroll bar to.)




                                      $("div").on("click", function(event) 
                                      event.preventDefault();
                                      var ScrollValue = $(document).width() - 100;
                                      $("html, body").animate(
                                      scrollLeft: ScrollValue
                                      , "slow");
                                      );


                                      ScrollValue can be calculated accordingly to your requirement and be passed as an integer value.



                                      Hope this solves your problem, For more info., refer: https://api.jquery.com/scrollleft/






                                      share|improve this answer













                                      Reason



                                      Reason why scrollLeft: $(document).width(50) didn't work because $(document).width(50) is not returning a valid number. (Since it is a setter method.)



                                      Fixes




                                      .scrollLeft( value ) Is expecting value of type: Number (An integer indicating the new position to set the scroll bar to.)




                                      $("div").on("click", function(event) 
                                      event.preventDefault();
                                      var ScrollValue = $(document).width() - 100;
                                      $("html, body").animate(
                                      scrollLeft: ScrollValue
                                      , "slow");
                                      );


                                      ScrollValue can be calculated accordingly to your requirement and be passed as an integer value.



                                      Hope this solves your problem, For more info., refer: https://api.jquery.com/scrollleft/







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Oct 30 '18 at 13:52









                                      Vishnu BaligaVishnu Baliga

                                      367411




                                      367411



























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53065263%2fjquery-width-function-parameters-not-working%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          這個網誌中的熱門文章

                                          Barbados

                                          How to read a connectionString WITH PROVIDER in .NET Core?

                                          Node.js Script on GitHub Pages or Amazon S3