Solved

CSS style sheet to dynamically created textbox using javascript

Posted on 2008-09-30
2
1,354 Views
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.

Thanks
<html>
 

<head>

<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);

  cellLeft.appendChild(textNode);

  

  // right cell

  var cellRight = row.insertCell(1);

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

  el.type = 'text';

  el.name = 'txtRow' + iteration;

  el.id = 'txtRow' + iteration;

  el.size = 40;

  

  el.onkeypress = keyPressTest;

  cellRight.appendChild(el);

  

  // select cell

  var cellRightSel = row.insertCell(2);

  var sel = document.createElement('select');

  sel.name = 'selRow' + iteration;

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

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

  cellRightSel.appendChild(sel);

}

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 = obj.id;

    } else {

      objId = this.id;

    }

    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 = window.open('', 'TableAddRowNewWindow',

   'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');

   

  // set the target to the blank window

  frm.target = 'TableAddRowNewWindow';

  

  // submit

  frm.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');

        return;

      }

    }

  }

  openInNewWindow(frm);

}
 
 

</script>

</head>
 

<body>

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

<p>

<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

</p>

<p>

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

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

</p>

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

  <tr>

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

  </tr>

  <tr>

    <td>1</td>

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

    <td>

    <select name="selRow0">

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

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

    </select>

    </td>

  </tr>

</table>

</form>
 

</body>
 

</html>

Open in new window

0
Comment
Question by:dotsandcoms
2 Comments
 
LVL 4

Accepted Solution

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

var el = document.createElement('input');
el.className="txtStyle";

then add that css to your page like this..

<style type="text/css">
.txtStyle
{
    color:Red;    
}
</style>
0
 
LVL 5

Expert Comment

by:jose_juan
ID: 22612060
Hi dotsandcoms,

what not work?

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

for example, put the next code

<style>
input {
    background-color: red;
}
</style>
</head>

you can see your inputs with background red.
0

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