We help IT Professionals succeed at work.

switching table attributes with JS

seamusseamus
seamusseamus asked
on
Medium Priority
213 Views
Last Modified: 2011-09-20
Hi there,

I'm trying to do a merge of the following two scripts:


#1:
------------------------------------------------------
<body onload="classChange();">
<script type="text/javascript">
<!--
function classChange() {
      for (i=0;i<document.getElementsByTagName("p").length; i++) {
            if (document.getElementsByTagName("p").item(i).className == "main"){
                  document.getElementsByTagName("p").item(i).style.color = "red";
            }
      }
}
//-->
</script>


#2:
------------------------------------------------------
function changeSize(px){
   var table = document.getElementById("tableMain2");
   table.setAttribute("width",px);
}


The issue is that I need to change the hard coded width of a table that only has class="main" attributed to it.  Unfortunately, I cannot change the HTML code.

Thanks!

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2006
Commented:
You can use:

function tableMainChange() {
for(i = 0; i < document.getElementsByTagName("table").length;i++)
          if (document.getElementsByTagName("table").item(i).className == "main"){
               document.getElementsByTagName("table").item(i).setAttribute("width", "100px");
          }
}
}

-r-

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
sorry for the delay in accepting this answer... worked like a charm!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.