Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prototyping document.getElementById 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
I tried googling but because of those bods who named their framework the same, I can't find what i need.

Can someone help me please, thanks..

here is what I have..

Code:
document.prototype.getid = function(myid) {return this.document.getElementById(myid);};

so then all I have to type is getid('myid').value

at the moment when i use getid it throws an object expected error.


thanks 1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
You don't need to use prototype, or document, if you are simply wanting to create a global function. Simply create a global function:

Code:
<html>
<head>
	<script type="text/javascript">

		[!]function getid(id) {
			return(document.getElementById(id));
		}[/!]

		window.onload = function() {
			alert(getid('wibble'));
		}

	</script>
</head>
<body>
	<div id="wibble"></div>
</body>
</html>

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
yeah I guessed that would be the easy option, but thought I'd try something new, Khat brought me the world of prototype and so thought I'd have ago.

is it possible with the prototype JS functionality and what would the syntax be?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
If you use prototype then you can't simply use "getid()", you have to use "document.getid()" because you're adding the method to the document object.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
ahh gotcha. although I tried document.getid() and still got an error, "object doesn't support this property or method". oh well I tried, I failed - lol.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I'm not sure why it didn't work when you referenced it properly. It should have. You might have had something else wrong with it.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top