tags:

views:

79

answers:

4

Hi aLL, i Wan't to know that what does getElementById Do in js?

we also use that in CrossSiteScripting attacks,

thanks

A: 

it gets a DOM ElementById

fuzzy lollipop
+5  A: 

Most html constructs have an element called "id" which must be unique in the whole html page. Such as <div id="uniqueDiv1">. getElementById returns that HTML object. In my example, getElementById("uniqueDiv1") returns that div. You can then use it to set a style or do something with it.

laura
And in Internet Explorer 6/7 it'll return an element whose "name" attribute has that value too. In other words, Internet Explorer is broken in this case. (I think 8 fixes this, but I'm not 100% sure.)
Pointy
A: 

It returns an element from the HTML document that has the ID attribute set to the value you ask for. For example, the JavaScript

document.getElementById('myId');

will return the first element found that matches that ID, such as

<a id="myId">Link</a>

I say the "first element found" because that's exactly what will happen--by definition, IDs must be unique in the page. Each HTML page must have only 1 ID of the same name. If you violate this rule, you'll get unexpected results.

Andrew Dunkman
A: 

getElementById allows you to access elements of a page.

Lets use this page as an example:

<html>
<head>
<title>Test Page</title>
</head>
<body>
<div id="myDIV">Hello World!</div>
</body>
</html>

document.getElemenById('myDIV').innerHTML will give you Hello World! document.getElementById('myDIV').style.display = "none" will hide the div tag.

If you want to look at what all it can do on a live page I would suggest getting Firefox and install the Firebug addon. This will let you see all of the DOM things and play with them without having to re-save a page.

Badger