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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.