javascript - change style of <texarea> using getElementsByTagName textAlign from center to left - Stack Overflow

I have a script to change the text alignment for the textarea with the id textbox1 below: <![C

I have a script to change the text alignment for the textarea with the id textbox1 below:

 // <![CDATA[
 function alignFix() {

 document.getElementById("textbox1").style.textAlign="left";

 }
 // ]]>

Here's the markup:

 <textarea cols="36" rows="25" readonly="readonly" id="textbox1" name="textbox" style="text-align: center;">dynamic text populates via another script unrelated to problem</textarea>

Here's the trigger:

 <select class="c9" onchange="showCenterTemplates(this); alignFix();">

It works just fine. Now I have more than one textarea I need this script to work on, so I thought it would be a simple switch from document.getElementById to document.getElementsByTagName but to my ignorance/surprise, that didn't work so well.

I've searched the questions and forums and Google, and have found examples of document.getElementsByTagName in action, but not in the manners I need it to.

It seems like when using getElementsByTagName, one must always declare a variable (can anyone confirm if this is true?) so I tried:

 // <![CDATA[
 function alignFix() {

 var textbox = document.getElementsByTagName('textarea');
 style_textbox = textbox.style;
 style_textbox.textAlign="left";
 }
 // ]]>

but with that I'm getting a style_textbox is null or not an object error when testing. Can anyone help me out please? Thanks very much in advance.

P.S. The reason for the script is because the original contents of the textareas need to be centered, but when the user begins to select templates from the <select> to populate dynamically in the <textarea> using the showCenterTemplates() script, those templates need to have left aligned text inside the <textarea>. Hope that makes sense.

I have a script to change the text alignment for the textarea with the id textbox1 below:

 // <![CDATA[
 function alignFix() {

 document.getElementById("textbox1").style.textAlign="left";

 }
 // ]]>

Here's the markup:

 <textarea cols="36" rows="25" readonly="readonly" id="textbox1" name="textbox" style="text-align: center;">dynamic text populates via another script unrelated to problem</textarea>

Here's the trigger:

 <select class="c9" onchange="showCenterTemplates(this); alignFix();">

It works just fine. Now I have more than one textarea I need this script to work on, so I thought it would be a simple switch from document.getElementById to document.getElementsByTagName but to my ignorance/surprise, that didn't work so well.

I've searched the questions and forums and Google, and have found examples of document.getElementsByTagName in action, but not in the manners I need it to.

It seems like when using getElementsByTagName, one must always declare a variable (can anyone confirm if this is true?) so I tried:

 // <![CDATA[
 function alignFix() {

 var textbox = document.getElementsByTagName('textarea');
 style_textbox = textbox.style;
 style_textbox.textAlign="left";
 }
 // ]]>

but with that I'm getting a style_textbox is null or not an object error when testing. Can anyone help me out please? Thanks very much in advance.

P.S. The reason for the script is because the original contents of the textareas need to be centered, but when the user begins to select templates from the <select> to populate dynamically in the <textarea> using the showCenterTemplates() script, those templates need to have left aligned text inside the <textarea>. Hope that makes sense.

Share Improve this question asked Sep 10, 2011 at 0:44 sixtwowaifusixtwowaifu 7974 gold badges17 silver badges41 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

getElementsByTagName returns an array of elements, so you have to loop over it:

var i,j, textbox = document.getElementsByTagName('textarea');
for (i=0, j=textbox.length; i<j; i++) {
    textbox[i].style.textAlign='left';
}

EDIT: per request in the ment, a short explanation:

  • i is incremented from 0 (zero) as long it doesn't reach the size of the array
  • textbox is the array, textbox.lenght returns its size
  • for every i (as in i is 0, i is 1 etc.) textbox[i] represents a position in the array
  • since the array is filled with HTML Elements, every position in the array is an Element, and has a style attribute

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745634654a4637316.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信