0

I want to hide certain columns of a HTML template with JavaScript. Could anybody tell me how can I achieve this? I've tried giving IDs to the entire column TDs and then making document.getElementById("ID").style.display = "none" but it hides only the first TD.

Thank you.

1
  • 2
    An id has to be unique. Use class instead. Commented Mar 4, 2011 at 21:44

3 Answers 3

4

Have you considered using the jQuery Library?

$('.tdCssClass').hide()

You could even go one step further and avoid the use of a cssClass:

$('td:nth-child(2)').hide()

Would hide the 2nd column.

Sign up to request clarification or add additional context in comments.

3 Comments

Still won't be able to hide all of the TDs that make up a given column this way.
Indeed, I realised my mistake shortly after submitting the answer.
Yep, and I like your second approach even better. Downvote changed to upvote.
1

IDs are only supposed to be used uniquely, i.e. for one element only, therefore getElementById will only return 1 element, the first that matches. Try using a class instead and:

var elems = document.getElementsByClassName("someClass");
for(var i = 0; i < elems.length; i++)
    elems[i].style.display = "none";

EDIT: in jQuery mode on accident >:)

Comments

0

I recently used jquery columnManager http://p.sohei.org/jquery-plugins/columnmanager/ to allow user to easily show/hide table columns and it worked very well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.