views:

285

answers:

2

I have a table within a div with a set height, when the table is bigger than the div the scroll-y bar appear. The problem is that if the scroll bar of the div is at the top, leaving other row at the bottom hidden, and the user press the button to add a new row, this row is added at the bottom of the table, therefore you can't see it.

Is there anyway with javascript, vbscript, or css to set focus to the row it's been created so it is visible when added?

This is the code where the function to add a new row would be called.

   strRow = strRow + "<div>
        <input ID='AddRowTableNew_" + cstr(newrow.id) + "' type='button' 
        value='Add New Row' 
        onclick='vbscript:AddNewRow 0," + cstr(newRow.id)+ "'/>
    </div>"

Any help would be appreciated.

PD: I would need this to be on VBScript or JavaScript, No JQuery please.

Thanks.

+1  A: 

Try this ...

var div = document.getElementById('the_div');
div.scrollTop = div.scrollHeight;

Hope it helps.

aefxx
This would fail when the scrollable area is more than 2x the offset height. If anything it should be `div.scrollTop = div.scrollHeight`'.
Andy E
My bad. Thanx Andy E.
aefxx
+3  A: 

Try Element.scrollIntoView();. It works in Firefox, IE6+, Opera and Google Chrome/Safari.

You can also provide a boolean to the function which will specify where to scroll the element to:

// Align the element with the top of the parent
Element.scrollIntoView();
Element.scrollIntoView(true);

// Align the element with the bottom of the parent
Element.scrollIntoView(false);

EDIT: MDC documentation for scrollIntoView with an example

To scroll to the bottom, you could also just set the element's scrollTop property to equal the scrollHeight property:

Element.scrollTop = Element.scrollHeight;
Andy E
Hi Andy, this it would be quite convenient for what I need, but I am having a small problem. I have This (onclick="vbscript:AddNewRow()".Do you know how can I add that statement into the onclick event? ex.(onclick="vbscript:AddNewRow();Element.scrollIntoView(false);"
Cesar Lopez
if the AddNewRow would return the element that was created (in the DOM) then you could do `vbscript: (AddNewRow()).scrollIntoView(false);`
Gaby
@Cesar Lopez: It would be easier to add the code to the end of your `AddNewRow` function. Just substitute `Element` with a reference to your newly created row eg. `newRow.scrollIntoView(false)`. Alternatively, you could have the function returning a reference to the newly created row and have your code like this: `onclick="vbscript:(AddNewRow()).scrollIntoView(false)"`
Andy E
Hi Gaby, I tried that, I think I would be best to paste the real onclick. onclick='vbscript:AddRegularDoseTimes 0," + cstr(newRow.id)+ "'How can I add that in there?
Cesar Lopez
Hi Andy and Gaby, many thanks to both of you, I've got it working with your help.Thanks a lot.
Cesar Lopez
This has one drawback though, it would scroll the outer page as well, not just the contents of the div.
aefxx
@jockelmog: Yes, if the page has vertical scrollbars, it would scroll those too. `Element.scrollTop = Element.scrollHeight` on the parent `div` would be the correct solution to use in that situation.
Andy E