views:

196

answers:

7

I need to hide all the tags which has the ID 'media'

E.g.:

`<div id='media'>contents</div>`

`<div id='media'>content</div>`

How to do this using JavaScript?

+7  A: 
document.getElementById("media").style.display = 'none';

As stated in the comments, you really should only use a particular ID value once per page. Use classnames for multiple elements.

Jonathan Sampson
thankyou, i will change it to class
RSK
A: 

You could do it fairly easily with jQuery.

For example, jQuery allows you to select all attributes with same ID, then apply some style or attribute to them in one action.

You could use the following. Then when you call the hide function from anywhere in your script, the media elements will be hidden.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"
     language="javascript" type="text/javascript">
</script>
<script>

 var hide = function() {
      $("#media").css("visibility", "hidden");
    }

</script>
ecounysis
Don't say you could do something 'fairly easily' in any language if you aren't going to write the 'fairly easy' example code to show how 'fairly easy' it is. I don't care what it is, if you're going to tell me it's 'fairly easy', I would expect a well written, concise explanation and implementation of this 'fairly easyness'.
Sneakyness
visibility: hidden is definitely not what most people would be looking for when they want to make elements disappear, as visibility hidden still allows the object to take up space.
Tegeril
+2  A: 

Normally you would use

document.getElementById('media').style.display = 'none';

if you want the screen real estate freed up. Or

document.getElementById('media').style.visibility = 'hidden';

if you want the whitespace still there.

But this won't work if you have more than one element on the page with id='media'. Consider having unique ids. It's bad practice not to. The way you're using id, people normally use class. If I were you, I would change them to class. But in that case getElementById() would not be suitable to solve your problem.

Asaph
+3  A: 

First of all, you really want to make your "id" attributes unique. You can use "name" instead.

Then, you can get an array of them using document.getElementsByName('media')

Finally, loop through and call...whatever it is you use to hide elements in JS. Set display to none, I think.

danben
+3  A: 

Hai Try this,

element = document.getElementById("yourcontrol");
if (element.id == "media")
 document.GetElementById(element).style.visibility = "Hidden"

or

document.GetElementById(element).style.display= "none"
Pandiya Chendur
+2  A: 

What BranTheMan said is true. You should probably use class instead of id. Then, if you want to use jQuery, you could simply do this:

$('.media').css("display", "none");
Jacob
+8  A: 

WOAH slow down there for a second buddy!

ID = IDENTIFIER = UNIQUE

You can only use id's once #id

CLASS = CATEGORY/GROUP = NOT UNIQUE

You can use classes everywhere .class as many times as you'd like.

Don't make classes named .id, that's silly.
Don't make an id named #class, as that is also silly.

document.getElementByClassName("class").style.display = 'none';

That'll do it.

Apparently no it won't, read this SO Question about the problem.


jQuery Implementation

Note: All the below examples assume that .hidden is defined in a css file like so:

.hidden {
    display: none;
}


Hiding Stuff

to hide something by id:

$("#id").addClass("hidden");

to hide something by class:

$(".class").addClass("hidden");

to hide everything inside of something:

$("#specificSomething *").addClass("hidden");

to hide everything of class bacon within the id stomach:

$("#stomach .bacon").addClass("hidden");


Showing Stuff

to show everything that is hidden:

$(".hidden").removeClass("hidden");

to show everything that is hidden inside of something:

$("#specificSomething .hidden").removeClass("hidden);


Don't forget!

You can also use the built in functions, show() and hide(), like so:

$("#specificsomething").hide();

$("#specificsomething").show();

They also have some fancyness built in, like basic animation. Refer to the documentation to use those.

It's best not to mix and match both of these methods, you can easily confuse yourself and waste plenty of time chasing silly mistakes.


Don't limit yourself!

If you're new to or unfamiliar with jQuery, don't forget that you can do things like:

  • Hide anything with a specific color.
  • Check to see if the first letter of a <p> is a vowel, and hide the entire <p>.
  • Hide all the vowels, everywhere, ever.
  • Hide anything that the user clicks on (when elaborately animated, is quite fun).

These are just a few (admittedly silly) examples of the magic jQuery is capable of. There's a whole book full of new and crazy ways to do things. It's called the documentation. Don't be afraid of it because it's large and full of words. Dive in! There are examples for EVERYTHING. Try things just to try them, even if you don't think they'll work (you'll be surprised).

I'll only give you the easiest of the five examples. I'll likely come back to this later and stuff it with more awesomeness.

Hide anything when the user clicks on it:

$("*").click(function () { 
     $(this).slideUp(); 
});


Wisdom Alert!

Remember, just because you aren't doing it right, doesn't mean you're doing it wrong. Sometimes, you aren't even doing it at all.

Cheesy Pun Warning!

Remember, it's always better to stay classy.

Sneakyness
Thankyou, it is very helpfull
RSK
You mean document.getElementsByClassName() and it's not available in IE: http://www.quirksmode.org/dom/w3c_core.html#t11Libraries like jquery implement this functionality for you in IE.
Annie
Are you sure about this? `document.getElementByClass()`? Since when does this function exist? And it returns a single element? I've heard of `document.getElementsByClassName()` that returns an array but that's not cross browser compatible.
Asaph
`document.getElementByClassName()` is not reliable in a cross browser context: See http://stackoverflow.com/questions/900117/is-getelementbyclass-safe-to-use-across-browsers-like-getelementbyid
Asaph
@Sneakyness: This is still wrong. There is *no such function* as `document.getElementByClassName()`. You're confusing it with `document.getElementsByClassName()`. And the latter returns an array which you would have to loop through and operate on each element. And anyway it's not cross browser compatible. See above comments.
Asaph
Terribly sorry for these continued mishaps, my Javascript knowledge has been spoiled by having worked so much in nothing but jQuery and ObjC. Should I even bother writing code for this?
Sneakyness
@Sneakyness: You have highest voted answer and it has been marked as correct by the asker. Yet it has known problems. You should at least remove the parts of it that you know to be incorrect. If you're handy with jQuery, you can provide a jQuery based answer?
Asaph
ugghhhhhhhh It's 01:00. You should really look at the size of the question I was working my way through today, I've literally been on this website editing text for a solid 1/12th of the day. I'm ready to come forward and say that I have a problem. :P
Sneakyness
Would love to hear what you have to say here: http://stackoverflow.com/questions/1917793/degradation-of-skills-as-a-result-of-javascript-libraries
Tegeril
Well there you go. You can resume upvoting ;]
Sneakyness