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

相关推荐

  • 3、win10重装系统后Mysql环境和数据的恢复

    因为电脑是机哥的原因&#xff0c;重装了好几次电脑&#xff0c;因为我习惯把软件都装在D盘。所以很多东西都还比较好恢复&#xff0c;在网上学会了怎么不卸载重装数据库&#xff0c;自己记录以备后面自己查

    55分钟前
    00
  • 推荐一个轻量级的监控平台并且支持移动端

    简介XUGOU 是基于Cloudflare构建的轻量化监控平台,专精于系统资源监控与可视化状态页面服务。该平台提供英文简体中文双语支持,满足全球化部署需求。面向开发者及中小团队,项目致力于提供高可用性的监控解决方案。核心功能与实现平台功能

    46分钟前
    00
  • 拥抱国产化:转转APP的鸿蒙NEXT端开发尝鲜之旅

    本文由转转技术团队赵卫兵分享,原题“鸿蒙新篇章:转转 APP 的 HarmonyOS Next 开发之旅”,下文进行了排版优化和内容修订。1、引言2023 年在华为开发者大会(HDC.Together)上,除了面向消费者的 HarmonyO

    40分钟前
    00
  • 在VMware虚拟机中安装Windows 7全攻略(避坑指南)

    ⚠️写在前面 最近发现很多开发者在调试老旧系统时都需要用到Windows 7环境&#xff08;特别是银行、医疗等行业的遗留系统&#xff09;&#xff0c;但实体机安装既不现实也不安全。今天就手把手教你在虚拟机

    35分钟前
    00
  • Nat. Mater.

    大家好,今天给大家分享一篇近期发表在Nat. Mater.上的研究进展,题为:De novo design of self-assembling peptides with antimicrobial activity guided

    33分钟前
    00
  • 雨晨 22635.5170 Windows 11 企业版 23H2 轻装版

    文件: 雨晨 22635.5170 Windows 11 企业版 23H2 轻装版 install.esd 大小: 2920270404 字节 修改时间: 2025年4月8日, 星期二, 11 : 04 : 59 MD5: D5F8F0AD

    32分钟前
    00
  • ​2025 轻松部署 Odoo 18 社区版

    随着 Odoo 18 社区版的发布,越来越多的企业希望借助这款开源 ERP 系统实现数字化转型。本文将深入解析传统部署方式的底层逻辑,并揭示如何通过自动化工具实现零门槛快速部署。一、手工部署 Odoo 18 技术全解 Docker 环境搭建

    26分钟前
    00
  • Java&amp;Activiti7实战:轻松构建你的第一个工作流

    本文已收录在Github,关注我,紧跟本系列专栏文章,咱们下篇再续!

    24分钟前
    00
  • 人工智能应用领域有哪些

    人工智能应用领域有哪些一、引言随着科技的飞速发展,人工智能(AI)已经逐渐渗透到我们生活的方方面面,成为推动社会进步的重要力量。从医疗健康到金融服务,从教育学习到智能制造,人工智能以其独特的技术优势,为各行各业带来了前所未有的变革。本文旨在

    22分钟前
    10
  • 【赵渝强老师】创建PostgreSQL的数据库

    在PostgreSQL中,创建数据库主要通过SQL命令“create database”完成,视频讲解如下:下面是具体的操作步骤。(1)查询现有数据库的集合,可以检查系统目录pg_database。代码语言:sql复制postgres=#

    19分钟前
    00
  • 在Windows上使用MetaMCP的完整指南

    在当今AI助手工具快速发展的时代&#xff0c;如何有效管理各种MCP&#xff08;Model Control Protocol&#xff09;服务成为了一个挑战。MetaMCP应运而生&#xff0c;它是

    18分钟前
    00
  • win11家庭版改为专业版

    找到“我的电脑”--“设置”--“系统”--“激活”--“更改密钥” 输入密钥“G49HN-9YQCT-684C3-R7T3F-3DBQB 即可成功。

    16分钟前
    00
  • VoidZero 的野心,开发者的福音!

    前言昨天分享了尤雨溪公司 VoidZero 最新的产品 TSDown,我相信肯定有同学和我一样好奇,尤雨溪为什么要推出这么多工具,来增加大家的学习压力!今天我们从整体上分析下,这些产品的功能和目的!正文VoidZero 是尤雨溪于 2020

    16分钟前
    00
  • 电子产品设计与电源优化实用策略

    产品降成本是商业活动中的常见行为,可贯穿于产品设计、研发、生产、运输、销售及维护的各个环节。然而,降成本策略必须建立在对产品品质要求不降低的基础上,确保设计参数满足要求并通过相关测试。以下是具体优化与深度分析。研发工程师通常从设计入手,选择

    14分钟前
    00
  • IDEA 2025.1 版震撼发布,建议更新

    好久不见甚是想念,有小伙伴催更了。 各位编程大神们!今天我要给大家带来一个超级劲爆的消息!IntelliJ IDEA 2025.1 终极版震撼发布啦!就像一颗超级炸弹在编程界炸开,这些新特性能让咱们的开发效率直接翻倍,简直爽歪歪

    8分钟前
    00
  • 连Claude 3.5都败下阵来,大语言模型能否定位软件服务的故障根因?

    论文的第一作者是香港中文大学(深圳)数据科学学院三年级博士生徐俊杰龙,指导老师为香港中文大学(深圳)数据科学学院的贺品嘉教授和微软主管研究员何世林博士。贺品嘉老师团队的研究重点是软件工程、LLM for DevOps、大模型安全。大型语言模

    6分钟前
    00
  • VMware 新建虚拟机并安装 Windows 10 系统全流程

    1.选择虚拟机硬件兼容性&#xff1a; 保持默认的 “Workstation 17.x”&#xff0c;勾选 “ESX Server”&#xff08;若需兼容&#xff09;&#xff0c;点击【下

    5分钟前
    00
  • 142页长文揭秘DeepSeek

    新智元报道编辑:定慧【新智元导读】DeepSeek-R1是近年来推理模型领域的一颗新星,它不仅突破了传统LLM的局限,还开启了全新的研究方向「思维链学」(Thoughtology)。这份长达142页的报告深入剖析了DeepSeek-R

    3分钟前
    00
  • 精选 5 款基于 .NET 开源、功能强大的编辑器

    前言今天大姚给大家分享 5 款基于 .NET 开源、免费、功能强大的编辑器,其中包含 C# 代码编辑器、文本编辑器、CSV 文本编辑器和 Markdown 编辑器。NetPadNetPad是一个基于.NET开源(MIT License)、跨

    1分钟前
    00
  • Windows 10 LTSC 2019 中文版下载及安装教程(附安装包)

    &#xff08;cn_windows_10_enterprise_ltsc_2019_x64_dvd_9c09ff24&#xff09;涵盖常见疑问和注意事项&#xff1a; cn_windows_10_enterpr

    1分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信