Jaxyral
04-19-2010, 10:13 PM
So it's been about a year since i did javascript and am doing some simple things to try and remember it again, I have never really used the DOM before either and this is where I am starting.
Here is my HTML page for now. I am trying to toggle between styles but i can only change the p[1] to toggle. p[0] toggles on the first click then stops toggling after. I do plan on making more styles so each p[1] p[0] h1 need to be accessed separately
.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>StyledDOM-v2</title>
<link rel="stylesheet" type="text/css" href="../css/styledDOM.css" />
</head>
<script type="application/javascript" src="../js/styledDOM.js"></script>
</head>
<body>
<h1 class="normal">Awesome</h1>
<p class="normal">Not as awesome as /..</p>
<p class="normal">Not as awesome as /.. or ../..</p>
<button onclick="toggleStyles()">Toggle Styles</button>
</body>
</html>
<body>
</body>
</html>
Here is the Javascript code
function toggleStyles() {
var h1 = document.getElementsByTagName("h1")[0];
var p0 = document.getElementsByTagName("p")[0];
var p1 = document.getElementsByTagName("p")[1];
p0.className = (p0.classname == "styled") ? "normal" : "styled";
p1.className = (p1.className == "styled") ? "normal" : "styled";
}
Here is the stylesheet
@charset "UTF-8";
.normal {
color: #008000;
font-style: normal;
font-weight: normal;
text-decoration: none;
}
.styled {
color: #FF0000;
font-style: italic;
font-weight: bold;
text-decoration: underline;
}
Here is my HTML page for now. I am trying to toggle between styles but i can only change the p[1] to toggle. p[0] toggles on the first click then stops toggling after. I do plan on making more styles so each p[1] p[0] h1 need to be accessed separately
.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>StyledDOM-v2</title>
<link rel="stylesheet" type="text/css" href="../css/styledDOM.css" />
</head>
<script type="application/javascript" src="../js/styledDOM.js"></script>
</head>
<body>
<h1 class="normal">Awesome</h1>
<p class="normal">Not as awesome as /..</p>
<p class="normal">Not as awesome as /.. or ../..</p>
<button onclick="toggleStyles()">Toggle Styles</button>
</body>
</html>
<body>
</body>
</html>
Here is the Javascript code
function toggleStyles() {
var h1 = document.getElementsByTagName("h1")[0];
var p0 = document.getElementsByTagName("p")[0];
var p1 = document.getElementsByTagName("p")[1];
p0.className = (p0.classname == "styled") ? "normal" : "styled";
p1.className = (p1.className == "styled") ? "normal" : "styled";
}
Here is the stylesheet
@charset "UTF-8";
.normal {
color: #008000;
font-style: normal;
font-weight: normal;
text-decoration: none;
}
.styled {
color: #FF0000;
font-style: italic;
font-weight: bold;
text-decoration: underline;
}