spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / mozillaapps / chap5 / 2 To page 1To page 2current pageTo page 4To page 5
[previous] [next]

Creating Applications with Mozilla, Chapter 5: Scripting Mozilla

Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


Changing an Element's CSS Style Using JavaScript

Much of what makes the Mozilla UI both flexible and programmable is its ability to dynamically alter the CSS style rules for elements at runtime. For example, if you have a button, you can toggle its visibility by using a simple combination of JavaScript and CSS. Given a basic set of buttons like this:

<button id="somebutton" class="testButton" label="foo" />
<spacer flex="1" />
<button id="ctlbutton"
class="testButton"
label="make disappear"
oncommand="disappear( );" />

as well as a stylesheet import statement at the top of the XUL like this:

<?xml-stylesheet href="test.css" type="text/css"?>

and a simple CSS file in your chrome/xfly/content directory called test.css that contains the following style rule:

#somebutton[hidden="true"]{ display: none; }
.testButton{
border            : 1px outset #cccccc;
background-color  : #cccccc;
padding           : 4px;
margin            : 50px;
}

You can call setAttribute in your script to hide the button at runtime.

<script>
function disappear( ){
return document.getElementById('somebutton').setAttribute('hidden', true);
}
</script>

The previous code snippet makes a visible button disappear by setting its hidden attribute to true. Adding a few more lines, you can toggle the visibility of the button, also making it appear if it is hidden:

<script>
function disappear( ){
const defaultLabel  = "make disappear";
const newLabel      = "make reappear";
var button          = document.getElementById('somebutton');
var ctlButton       = document.getElementById('ctlbutton');
if(!button.getAttribute('hidden')) {
button.setAttribute('hidden', true);
ctlButton.setAttribute('label', newLabel);
} else  {
button.removeAttribute('hidden');
ctlButton.setAttribute('label', defaultLabel);
}
return;
}
</script>

Another useful application of this functionality is to collapse elements such as toolbars, boxes, and iframes in your application.

The setAttribute method can also be used to update the element's class attribute with which style rules are so often associated. toolbarbutton-1 and button-toolbar are two different classes of button. You can change a button from a toolbarbutton-1-the large button used in the browser-to a standard toolbar button using the following DOM code:

// get the Back button in the browser
b1 = document.getElementById("back-button");\
b1.setAttribute("class", "button-toolbar");

This dynamically demotes the Back button to an ordinary toolbar button. Code such as this assumes, of course, that you know the classes that are used to style the various widgets in the interface.

You can also set the style attribute directly using the DOM:

el = document.getElementById("some-element");
el.setAttribute("style", "background-color:darkblue;");

Be aware, however, that when you set the style attribute in this way, you are overwriting whatever style properties may already have been defined in the style attribute. If the document referenced in the snippet above by the ID some-element has a style attribute in which the font size is set to 18pc, for example, that information is erased when the style attribute is manipulated in this way.


home / programming / javascript / mozillaapps / chap5 / 2 To page 1To page 2current pageTo page 4To page 5
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

Created: September 26, 2002
Revised: September 26, 2002

URL: http://webreference.com/programming/javascript/mozillaapps/chap5/2/3.html