CSS style sheet to dynamically created textbox using javascript

Posted on 2008-09-30
Last Modified: 2012-05-05
Hi floks,

I am creating the textbox dynamically using javascript. I am want to apply css style sheet to it and things are not working.

Plz advice.



<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<title>Validate Submit</title>

<script language="javascript">

function addRowToTable()


  var tbl = document.getElementById('tblSample');

  var lastRow = tbl.rows.length;

  // if there's no header row in the table, then iteration = lastRow + 1

  var iteration = lastRow;

  var row = tbl.insertRow(lastRow);


  // left cell

  var cellLeft = row.insertCell(0);

  var textNode = document.createTextNode(iteration);



  // right cell

  var cellRight = row.insertCell(1);

  var el = document.createElement('input');

  el.type = 'text'; = 'txtRow' + iteration; = 'txtRow' + iteration;

  el.size = 40;


  el.onkeypress = keyPressTest;



  // select cell

  var cellRightSel = row.insertCell(2);

  var sel = document.createElement('select'); = 'selRow' + iteration;

  sel.options[0] = new Option('text zero', 'value0');

  sel.options[1] = new Option('text one', 'value1');



function keyPressTest(e, obj)


  var validateChkb = document.getElementById('chkValidateOnKeyPress');

  if (validateChkb.checked) {

    var displayObj = document.getElementById('spanOutput');

    var key;

    if(window.event) {

      key = window.event.keyCode; 


    else if(e.which) {

      key = e.which;


    var objId;

    if (obj != null) {

      objId =;

    } else {

      objId =;


    displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);



function removeRowFromTable()


  var tbl = document.getElementById('tblSample');

  var lastRow = tbl.rows.length;

  if (lastRow > 2) tbl.deleteRow(lastRow - 1);


function openInNewWindow(frm)


  // open a blank window

  var aWindow ='', 'TableAddRowNewWindow',



  // set the target to the blank window = 'TableAddRowNewWindow';


  // submit



function validateRow(frm)


  var chkb = document.getElementById('chkValidate');

  if (chkb.checked) {

    var tbl = document.getElementById('tblSample');

    var lastRow = tbl.rows.length - 1;

    var i;

    for (i=1; i<=lastRow; i++) {

      var aRow = document.getElementById('txtRow' + i);

      if (aRow.value.length <= 0) {

        alert('Row ' + i + ' is empty');










<form action="tableaddrow_nw.html" method="get">


<input type="button" value="Add" onclick="addRowToTable();" />

<input type="button" value="Remove" onclick="removeRowFromTable();" />

<input type="button" value="Submit" onclick="validateRow(this.form);" />

<input type="checkbox" id="chkValidate" /> Validate Submit



<input type="checkbox" id="chkValidateOnKeyPress" checked="checked" /> Display OnKeyPress

<span id="spanOutput" style="border: 1px solid #000; padding: 3px;"> </span>


<table border="1" id="tblSample">


    <th colspan="3">Sample table</th>




    <td><input type="text" name="txtRow1" id="txtRow1" size="40" onkeypress="keyPressTest(event, this);" /></td>


    <select name="selRow0">

    <option value="value0">text zero</option>

    <option value="value1">text one</option>








Open in new window

Question by:dotsandcoms

Accepted Solution

sijishJohn earned 250 total points
ID: 22612053
You can apply css to dynamically created textbox like this...

var el = document.createElement('input');

then add that css to your page like this..

<style type="text/css">

Expert Comment

ID: 22612060
Hi dotsandcoms,

what not work?

put a css style, if not, what style must be applicated?

for example, put the next code

input {
    background-color: red;

you can see your inputs with background red.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now