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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On Load make javascript run 1

Status
Not open for further replies.

evr72

MIS
Dec 8, 2009
265
US
Hello,

I am working on a web report, I have a script that if the user checks the check box then it hides the column.
This works well, but I was wondering if there is a way that I could have all my columns hidden when the page loads, then if the user checks the check box the column will appear.

this is an example of the code that I have
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>



<script src="jquery-latest.js"></script> 

</head>

<body>

  <script> 
$(document).ready(function() { 
    $('input[type="checkbox"]').click(function() { 
        var index = $(this).attr('name').substr(3); 
        index--; 
        $('table tr').each(function() {  
            $('td:eq(' + index + ')',this).toggle(); 
        }); 
        $('th.' + $(this).attr('name')).toggle(); 
    }); 
}); 
</script> 



  <form name="myform">
    <p><br />
      <br />
      <br /> 

	  <input name="col4" id="1" type="checkbox"  /> 
	Hide/Show Column 4 
	<input name="col3" id="2" type="checkbox" />
	Hide/Show
	Column
	3
	<input name="col2" id="3" type="checkbox" />
Hide/Show Column
2  
    <input name="col1"id="4" type="checkbox" />
Hide/Show Column
1</p>
    <p><br /> 
        </p>
  </form> 
  <table> 
<thead> 
</thead> 
<tr>
  <td><input type="text" name="qty" value="10" /></td>
  <td><input type="text" name="prcbook"value="25" onchange="startCalc4();" /></td><td> <input type="text" name="total" value="" /></td><td><input name="Input" type="text" value="35" /></td></tr> 
</table>

</body>
</html>
 
Hi

Something like this ?
JavaScript:
$[teal]([/teal]document[teal]).[/teal][COLOR=darkgoldenrod]ready[/color][teal]([/teal][b]function[/b][teal]()[/teal] [teal]{[/teal]
    $[teal]([/teal][green][i]'input[type="checkbox"]'[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]click[/color][teal]([/teal][b]function[/b][teal]()[/teal] [teal]{[/teal]
        [b]var[/b] index [teal]=[/teal] $[teal]([/teal][b]this[/b][teal]).[/teal][COLOR=darkgoldenrod]attr[/color][teal]([/teal][green][i]'name'[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]substr[/color][teal]([/teal][purple]3[/purple][teal]);[/teal]
        index[teal]--;[/teal]
        $[teal]([/teal][green][i]'table tr'[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]each[/color][teal]([/teal][b]function[/b][teal]()[/teal] [teal]{[/teal]  
            $[teal]([/teal][green][i]'td:eq('[/i][/green] [teal]+[/teal] index [teal]+[/teal] [green][i]')'[/i][/green][teal],[/teal][b]this[/b][teal]).[/teal][COLOR=darkgoldenrod]toggle[/color][teal]();[/teal]
        [teal]}[/teal][teal]);[/teal]
        $[teal]([/teal][green][i]'th.'[/i][/green] [teal]+[/teal] $[teal]([/teal][b]this[/b][teal]).[/teal][COLOR=darkgoldenrod]attr[/color][teal]([/teal][green][i]'name'[/i][/green][teal])).[/teal][COLOR=darkgoldenrod]toggle[/color][teal]();[/teal]
    [teal]}[/teal][teal])[/teal][highlight][teal].[/teal][COLOR=darkgoldenrod]click[/color][teal]()[/teal][/highlight][teal];[/teal]
[teal]}[/teal][teal]);[/teal]

Feherke.
 
feherke!!!!!!!

YOU ROCK!!!!!!!!

that was so simple and I have been trying a bunch of stuff

I really appreciate it!!!!!
 
Hi

Note that my above code is just a hint. Do not use it. You need something "like" that. The [tt]click()[/tt] method will toggle the [tt]checkbox[/tt] state while you need to explicitly check it. Otherwise after a page reload the [tt]checkbox[/tt] states and the [tt]input[/tt] visibilities will be mixed.

Sorry, I not know jQuery and I really hate its documentation. You will have to search yourself for a suitable method to replace [tt]click()[/tt].

In meantime you could make a dirty trick like this :
JavaScript:
$[teal]([/teal]document[teal]).[/teal][COLOR=darkgoldenrod]ready[/color][teal]([/teal][b]function[/b][teal]()[/teal]  [teal]{[/teal]
    $[teal]([/teal][green][i]'input[type="checkbox"]'[/i][/green][teal])[/teal][highlight][teal].[/teal][COLOR=darkgoldenrod]attr[/color][teal]([/teal][green][i]'checked'[/i][/green][teal],[/teal][b]true[/b][teal])[/teal][/highlight][teal].[/teal][COLOR=darkgoldenrod]click[/color][teal]([/teal][b]function[/b][teal]()[/teal] [teal]{[/teal]
        [b]var[/b] index [teal]=[/teal] $[teal]([/teal][b]this[/b][teal]).[/teal][COLOR=darkgoldenrod]attr[/color][teal]([/teal][green][i]'name'[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]substr[/color][teal]([/teal][purple]3[/purple][teal]);[/teal]
        index[teal]--;[/teal]
        $[teal]([/teal][green][i]'table tr'[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]each[/color][teal]([/teal][b]function[/b][teal]()[/teal] [teal]{[/teal]  
            $[teal]([/teal][green][i]'td:eq('[/i][/green] [teal]+[/teal] index [teal]+[/teal] [green][i]')'[/i][/green][teal],[/teal][b]this[/b][teal]).[/teal][COLOR=darkgoldenrod]toggle[/color][teal]();[/teal]
        [teal]}[/teal][teal]);[/teal]
        $[teal]([/teal][green][i]'th.'[/i][/green] [teal]+[/teal] $[teal]([/teal][b]this[/b][teal]).[/teal][COLOR=darkgoldenrod]attr[/color][teal]([/teal][green][i]'name'[/i][/green][teal])).[/teal][COLOR=darkgoldenrod]toggle[/color][teal]();[/teal]
    [teal]}[/teal][teal]).[/teal][COLOR=darkgoldenrod]click[/color][teal]();[/teal]
[teal]}[/teal][teal]);[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top