Styling with javascript doesn't always work

I am styling the div using JavaScript, which appears ok the first time, but when I click on the image to open the virtual window, the applied CSS goes away.

How can I apply a style using a script that stays forever.

** I was unable to apply any style at design time because I am using an out-of-the-box custom control and I only get the ID for the DIV at runtime.

Any idea ??

Here is the script I am using ...

window.onload = changeDivOverflow;
function  changeDivOverflow()
{
   var treeDiv = document.getElementById('T_ctl00contentMainctl04tvw');
   treeDiv.style.overflow = "auto";
}

      

+1


a source to share


2 answers


All styles applied using javascript and DOM calls only apply to the element you apply it to. In your example, this will only be an element with ID = 'T_ctl00contentMainctl04tvw'

If you want to reuse this style across multiple elements, you must create a CSS class and store that style there. Then apply this class for every element you want. If you want to do it with a script, you need:



var treeDiv = document.getElementById('T_ctl00contentMainctl04tvw');
treeDiv.className = "your-class";

      

0


a source


Since you are using ASP.NET, but I cannot see all of your complete code, I can only assume that your DIV has a tag runat="server"

and why you cannot get the ID on the client side.

If so, you might want to consider the following change to get the treeDiv:



var treeDiv = document.getElementById("<%= tvw.ClientID %>");

      

+1


a source







All Articles