Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shortening script

Status
Not open for further replies.

TheConeHead

Programmer
Aug 14, 2002
2,106
0
0
US
How can I shorten this, the key is the expression and the colspans:

if (counter == 1) {
<td colspan="4">&nbsp;</td>
} if (counter == 2) {
<td colspan="3">&nbsp;</td>
} if (counter == 3) {
<td colspan="2">&nbsp;</td>
} if (counter == 4) {
<td colspan="1">&nbsp;</td>
}

[conehead]
 
<html>
<head>
<script language="JavaScript">
var counter=3;//sample value
var colspanvalue;
colspanvalue=5-counter;
</script>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="1">
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr>

<script language="JavaScript">
document.write('<td colspan=" ' + colspanvalue + ' ">&nbsp;</td>');
</script>
</tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
</table>
</body>
</html>
 
Is the total number of columns dynamic, or will there always be at most 4? Also, are you generating this server side or clients side? The code you supplied doesn't look like it would work for either, if you're wanting to do this client side something similar to what cvdtbrrsln posted will work. If you're wanting to do it server side, well.... we'll have to know what SSL you're using first. And whether the columns are dynamic too, and what variable the number of columns is stored in.

-kaht

banghead.gif
 
no... it is ss... the number of columns and options for "counters" values are set - they will be 1-4...

[conehead]
 
I'll post an example in asp (using server side JScript) using cvdtbrrsln's suggestion:
Code:
<html>
<head>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="1">
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr>
<%
//value from 1 to 4, I assume this is generated server side?
var counter = 1;
var colspanvalue = (5 - counter);
Response.Write("<td colspan=" + colspanvalue + ">&nbsp;</td>");
%>  
</tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
</table>
</body>
</html>

-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top