Posted by: CIGG June 16, 2008
Need help in Java Script and JSP
Login in to Rate this Post:     0       ?        

Here is the code: There are two files one html file and one js file

THE CODE BELOW GOES TO HTML FILE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

<title>Untitled Page</title>

<script language="javascript" type="text/javascript" src="TableScript.js"></script>

</head>

<body style="font-size: 8pt; font-family: Verdana, Arial" text="#000000">

<table border="0" cellpadding="3" cellspacing="3" width="60%">

<tr>

<td colspan="3">

&nbsp; &nbsp;&nbsp; <strong>

Create Table:</strong></td>

</tr>

<tr>

<td align="right" style="width: 40%">

Number of Rows:

</td>

<td colspan="2">

<input id="tdRows" size="5" type="text" maxlength="2" /></td>

</tr>

<tr>

<td align="right" style="width: 40%; height: 19px">

Number of Columns:</td>

<td colspan="2" style="height: 19px">

<input id="tdCols" size="5" type="text" maxlength="2" /></td>

</tr>

<tr>

<td align="right" style="width: 40%; height: 19px">

</td>

<td colspan="2" style="height: 19px">

<input id="btnGenerateTable" type="button" value="Generate Table" onclick="generateTable();" /></td>

</tr>

</table>

<div id="tableHolder" style="width: 100%">

</div>

<div id="Div1" style="display:none; visibility:hidden;width: 100%">

<input id="File1" type="file" /></div>

</body>

</html>

 

THE CODE BELOW GOES TO A JS FILE, FILE NAME SHOULD BE: TableScript.js

function generateTable()

{

var tableHolder = document.getElementById("tableHolder");

tableHolder.innerHTML = "";

var tdRows = document.getElementById("tdRows");

var tdColumns = document.getElementById("tdCols");

if(tdRows.value!="" && tdColumns.Value!="")

{

// creates a <table> element and a <tbody> element

var tbl = document.createElement("table");

tbl.setAttribute("width", "60%");

tbl.setAttribute("id", "dynamicTable");

var tblBody = document.createElement("tbody");

tbl.setAttribute("border", "1pt outset #CCC");

tbl.setAttribute("cellpadding", "3");

tbl.setAttribute("cellspacing", "3");

tbl.setAttribute("border-collapse", "collapse");

// creating all cells

for (var i = 0; i < tdRows.value; i++) {

// creates a table row

var row = document.createElement("tr");

for (var j = 0; j < tdColumns.value; j++) {

// Create a <td> element

var cell = document.createElement("td");

cell.style.background = "rgb(240,240,240)";

cell.setAttribute("align", "center");

cell.setAttribute("height", "15pt");

cell.setAttribute("id", "tbl"+i+j);

cell.innerText = cell.id+ ": Click Me";

cell.onclick = function (evt) { setElement(this);};

cell.style.cursor="hand";

row.appendChild(cell);

}

// add the row to the end of the table body

tblBody.appendChild(row);

}

// put the <tbody> in the <table>

tbl.appendChild(tblBody);

// appends <table> into <div>

tableHolder.appendChild(tbl);

}

}

function setElement(td)

{

document.getElementById("File1").click();

if(document.getElementById("File1").value!=null)

{

td.innerHTML = "<img src='"+document.getElementById("File1").value+"'>";

document.getElementById("File1").value="";

}

}

Read Full Discussion Thread for this article