How to align the value in a text box using javascript?

How to align the value in a text box using javascript?

I found various methods to do it on google, but only one of them worked for me. Hence I decided to write a post on this.




function mytest3() {
// This works in both IE and chrome.
document.getElementById("y1").style.textAlign="right";

// Not working.
// document.getElementById("y1").style="text-align:right";

// Following works in Chrome (But not in IE)
// document.getElementById("y1").setAttribute("style", "text-align:right");

}








Well, for more information why the other two options were not working.
Please have a look at the following URL under section Text Properties
http://www.comptechdoc.org/independent/web/cgi/javamanual/javastyle.html

Another good link to study is

http://www.webdeveloper.com/forum/showthread.php?t=81111

Well, if you like to use jquery and achive the same thing.




<title&gtjQuery demo
<script type="text/javascript" src="jquery.js" language="javascript"&gt


$(document).ready(function(){
$('#mydiv').attr("style","text-align:right");

});







Another very good website for learning javascript is http://jsbin.com/#javascript,html,live

About Vineet Verma

Developer/Blogger/Gamer/Lazy Couch Potato...:P Need PDF Books: Knowledge Base
This entry was posted in HTML. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s