Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 359677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:24:59+00:00 2026-05-12T12:24:59+00:00

I’m using jQuery to dynamically add two divs on page load. It all works

  • 0

I’m using jQuery to dynamically add two divs on page load. It all works great, except, when I click the “Advanced Options” link in the first dynamically loaded div – it performs it’s correct behavior (sliding the second dynamic div), BUT it resets the scroll of the window back to the top. I attempted to focus on the first input element of the div that I slide open, but to no avail.

How do I get rid of this undesired scrolling behavior?

I’ve included the code to help (as long winded as it is)

<style type="text/css">
.itemcontainer {
    background-color:#FFFFCC;
    border:1px solid #999999;
    height:auto;
    margin:1em 0;
    overflow:hidden;
    position:relative;
    width:600px;
    }
.itemcontainer .optionpopoutcontainer {
    background-color:#44CC55;
    bottom:0;
    color:#333333;
    height:auto;
    left:0;
    padding:6px;
    position:absolute;
    width:100%;
}
.itemcontainer .advancedpopoutcontainer {}
</style>

And here’s the javascript along with the HTML

    <script type="text/javascript">
    $(document).ready(function(){
        var counter = 0;
        //Add the necessary divs to the classes
        $(".itemcontainer").each(function(){
            var id = $(this).find("input[name='ItemID']").attr('value');
            $(this).append("<div class='optionpopoutcontainer' id='quickaddcontainer-" + id + "'></div><div class='advancedpopoutcontainer' id='advancedaddcontainer-" + id + "'></div>");
        });


            $(".optionpopoutcontainer").css("display", "none"); //used for the sliding up
        $(".optionpopoutcontainer").each(function(){
            createQuickAddForm($(this), GetID($(this)));
        });
        $(".advancedpopoutcontainer").css("display", "none"); //used for the sliding up
        $(".advancedpopoutcontainer").each(function(){
            AddAdvancedOptionsForm($(this), GetID($(this)));
        });


        $(".cancelButton").click(function(){
            var parent = GetParentDiv($(this));
            parent.slideToggle();
        });


        $(".opencontainer").click(function(){
            GetParentDiv($(this)).find("div.optionpopoutcontainer").slideToggle(250);
        });

        $(".quickaddadvoptions").click(function(){
            var container = GetParentDiv($(this));
            $(container).slideToggle(function(){
                var innerContainer = GetParentDiv(container).find("div.advancedpopoutcontainer").slideToggle(250);

            });
        });

        function GetParentDiv(item){
            return item.parents('div:eq(0)');
        }

        function GetID(item){
            return item.attr('id').split('-')[1];
        }


        $("input[name='Date']").each(function(){
            $(this).datepicker({dateFormat: 'd MM, y'});
        });
    });



    function createQuickAddForm(item, itemID){
            var str = "<form method='post' action='#' id='addform-" + itemID + "'>";
            str += "Size: <input id='Size' type='text' value='1' name='Size'/> ";
            str += "<input id='ItemID' type='hidden' value='29' name='ItemID'/>";
            str += "<input type='submit' value='Log Item'/>";
            str += "<a href='#'' class='quickaddadvoptions' id='advancedoptions-" + itemID + "'>Advanced Options</a>";
            str += "</form>";
            str += "<button  class='cancelButton'>Cancel</button>";

            item.html(str);
        };


    function AddAdvancedOptionsForm(containerDiv, itemID){
            var str = "<form method='post' action='#' id='addformadv-" + itemID + "'>";
            str += "Size: <input id='Size-" + itemID + "' type='text' value='1' name='Size'/><br/>";
            str += "Date: <input id='Date-" + itemID + "' type='text' value='" + GetTodaysDate() + "' name='Date'/><br/>";
            str += "Note: <input type='textarea' id='Note-" + itemID + "' name='Note' cols='20' name='Note'/><br/>";
            str += "<input id='ItemID-" + itemID + "' type='hidden' value='29' name='ItemID'/>";
            str += "<input type='submit' value='Log Item'/>";
            str += "</form>";
            str += "<button  class='cancelButton' >Cancel</button>";

            containerDiv.html(str);
        };
<div class="itemcontainer" name="item1">
<!-- <table> code -->
<button style="float:right" class="opencontainer">slide it</button>
<!-- other <table>  code -->
            <input type="hidden" value="2" name="ItemID"/>
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:25:00+00:00Added an answer on May 12, 2026 at 12:25 pm

    Try preventing the default action of your hyperlink from happening:

     $(".quickaddadvoptions").click(function(e){
                    e.preventDefault();
                    var container = GetParentDiv($(this));
                    $(container).slideToggle(function(){
                            var innerContainer = GetParentDiv(container).find("div.advancedpopoutcontainer").slideToggle(250);
    
                    });
            });
    

    That will prevent the browser from jumping when it sees the anchor ‘#’ in <a href='#'' of your dynamically loaded div. In other words, the anchor will behave more like a button than a hyperlink.

    See http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29

    Also note, a return false; at the end of that event handler should do the trick too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 209k
  • Answers 209k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer G'day, I know it sounds clunky, but you might have… May 12, 2026 at 9:40 pm
  • Editorial Team
    Editorial Team added an answer Look in the Request.Form collection to retrive those values something… May 12, 2026 at 9:40 pm
  • Editorial Team
    Editorial Team added an answer public class Alien extends Unit{ public void move(long delta){ System.out.println("2");… May 12, 2026 at 9:40 pm

Related Questions

In order to apply a triggered animation to all ToolTip s in my app,
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.