Javascript Smart element setter

This is a small function to help anyone that uses a lot of  the infamous “document.getElementById(‘id’)” in Javascript coding and with some small modification you can make this function even better.

The function will get an id as parameter and return the document element refering that id. Is simple as that and it can make your code more readable and easier to work. The only detail is, do not use it if you is already using JQuery or any other Javascript framework that have a similar method.

<script type=”text/javascript”>

function $(id){
var element;
if(null != document.getElementById(id)){
element = document.getElementById(id);
return element;
return false;


A small use for this function would be something like this:

<input type=”text” id=”text1″ name=”text1″ value=”” />
<input type=”file” id=”file1″ name=”file1″ value=”” onchange=”$(‘text1’).value = $(‘file1’).value;” />

Have fun.


About mcloide

Making things simpler, just check: View all posts by mcloide

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: