Post by Gamoc on Jan 7, 2007 0:30:48 GMT -5
Okay, The getElementById() and the getElementByTagName() are two very important methods when it comes to javascripting. These get elements will let you call something from a documenter by its id or its tag name, here, let me show an example of both.
Okay. Now we have out getElementById() there, well, what is it doing. What it basically did is take the div element we had there and stored it into a variable named something.
This doesn't have to be used with just divs, it can be with the Id's of a p tag, a body tag, but they have to have Id's, or the browser will call an error.
note to users: This is case sensitive, so document.getelementbyid() will cause and error
note to users: You must have document. before it or else it will not go through cause the browser won't know to look through the document for the id
Now onto the getElementByTagName, this one works differently, I will start with an example and explain it from there.
First you will see that we called all the <p> tags using the document.getElementByTagName. Wait, there is more than one <p> tag, no problem, first, you would loop through all of the loop tags. Then the if statement is used to match the <p> tag that you would wish to use here.
note to users: although this is useful, try to use document.getElementById() to save space and time.
Hope it helps you understand these better.
-Pikablu-
<html>
<head>
<title>JS example</title>
</head>
<body>
<div id="somediv"></div>
<scrpt type=text/javascript">
<!--
var something = document.getElementById("somediv")
-->
</script>
</body>
</html>
Okay. Now we have out getElementById() there, well, what is it doing. What it basically did is take the div element we had there and stored it into a variable named something.
This doesn't have to be used with just divs, it can be with the Id's of a p tag, a body tag, but they have to have Id's, or the browser will call an error.
note to users: This is case sensitive, so document.getelementbyid() will cause and error
note to users: You must have document. before it or else it will not go through cause the browser won't know to look through the document for the id
Now onto the getElementByTagName, this one works differently, I will start with an example and explain it from there.
<html>
<head>
<title>JS example</title>
</head>
<body>
<p>Hello there, how are you this fine day.</p>
<p>I am doing good.</p>
<script type="text/javascript">
<!--
var get_tag=document.getElementByTagName("p")
for(a=0;a<get_tag;a++)
{
if(get_tag[a].attribute=="attribute_value" && get_tag[a].innerHTML.match(text))
{
Do some code
}
}
-->
</script>
</body>
</html>
First you will see that we called all the <p> tags using the document.getElementByTagName. Wait, there is more than one <p> tag, no problem, first, you would loop through all of the loop tags. Then the if statement is used to match the <p> tag that you would wish to use here.
note to users: although this is useful, try to use document.getElementById() to save space and time.
Hope it helps you understand these better.
-Pikablu-