programmer-in-training Posted April 9, 2008 Share Posted April 9, 2008 How do I find the pixel equivalent of an "em?" Link to comment Share on other sites More sharing options...
Ingolme Posted April 9, 2008 Share Posted April 9, 2008 There really isn't a way. The "em" is set by the default font size of the user's browser. If you want pixel values just use px as a unit instead.Though...Maybe with javascript you can detect it, something like this: <div style="height: 1em" id="check"></div><script type="text/javascript"> var emSize = document.getElementById("check").offsetHeight; // Use the variable "emSize" wherever you need the pixel value of the em unit</script> Link to comment Share on other sites More sharing options...
programmer-in-training Posted April 9, 2008 Author Share Posted April 9, 2008 That works. I prefer to have "visibility: hidden" and var emSize = check.offsetHeight; do I really need all that document.getElementById() confusion?Also, how do I find the height of the <td> I'm in? (I'm trying to subtract 1 em from 100% height.) Link to comment Share on other sites More sharing options...
programmer-in-training Posted April 9, 2008 Author Share Posted April 9, 2008 EDIT: That was a nice function to find the em size, but I realized I actually don't need it! Instead of "100%" I put in "menu.offsetHeight" and now it works. Thanks though it will be very useful. I've had to use less efficient workarounds before I came to these forums! Wonder why I never noticed them at w3schools before.... :)Also, I see why document.getElementById() would be useful if you're searching for a string. Link to comment Share on other sites More sharing options...
Synook Posted April 10, 2008 Share Posted April 10, 2008 Using the element's ID directly as an object without an assignment is not really correct, and isn't part of the W3C standard. Always use document.getElementById(). menu.offsetHeight; //Incorrect (though will work)menu = document.getElementById("menu"); //Correctmenu.offsetHeight; //Now also correct because of the assignment on the line above By the way, an "em" is the width of the letter m at the current font size. Link to comment Share on other sites More sharing options...
jeffman Posted April 10, 2008 Share Posted April 10, 2008 My version of a little routine that saves a lot of typing: function gg(id) { return document.getElementById(id);}//call it like this:gg("id"); A lot of people use a $ instead of gg, but that makes my perl/php confusing. I remember "gg" by thinking of it as "go get." Link to comment Share on other sites More sharing options...
Ingolme Posted April 10, 2008 Share Posted April 10, 2008 "$" is a valid function name in Javascript? Link to comment Share on other sites More sharing options...
jesh Posted April 10, 2008 Share Posted April 10, 2008 No, but it is in jQuery - which seems to be fairly popular.http://jquery.com/ Link to comment Share on other sites More sharing options...
jeffman Posted April 10, 2008 Share Posted April 10, 2008 Actually, it is. C/P this to be sure. function $(){ alert ("Hello");}$(); I'm surprised you haven't seen this. It's in dozens of "My top-ten Javascript tips" pages around the net. I suppose it could stop being legal without warning; I don't really know. And as I said, I don't use it. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 10, 2008 Share Posted April 10, 2008 Yeah, like DD said there are quite a few libraries that define the $ function to replace document.getElementById. It confused the crap out of me until I realized that $ was a legal function name, I was assuming it was some operator where everyone but me got the memo. function $(s){ return document.getElementById(s);}val = $('elId').value;$('elId').style.display = 'none'; It just makes the code a little cleaner, it looks more like you're working directly with the object instead of working with document.getElementById. Link to comment Share on other sites More sharing options...
jesh Posted April 10, 2008 Share Posted April 10, 2008 Actually, it is.Ahh, right. I knew it was a valid function name. I had assumed that the OP was asking if "$" was a built in feature of javascript/DOM like "alert", "getElementById", etc. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.