views:

633

answers:

1

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>
+2  A: 

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.

karim79
Don't know how I missed that. Thanks, I appreciate the help.
MunkiPhD