Click here to Skip to main content
15,891,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i found this javascript is called by a button
but i want it to be onload at the same time , when button click it will call this javscript as well
but i not sure how? hopefully anyone of you can help out

JavaScript
function AddFileUpload()
         {
              var div = document.createElement('DIV');
              div.innerHTML = '<input id="file' + counter + '" name = "file' + counter + '" type="file" /><input id="Button' + counter + '" type="button" value="Remove" onclick = "RemoveFileUpload(this)" />';
              document.getElementById("FileUploadContainer").appendChild(div);
              counter++;
         }
Posted
Comments
nbeiruty 2-Feb-12 7:05am    
Hello ,
can u be more specific , what do u want exactly that onload of the website this div appear? or onclick?

Hi,

Try this simple solution that is using onload event on body tag...
HTML
<html>
<head>
	<script>
		
	var counter = 0;

	function AddFileUpload()
	{
		var div = document.createElement('DIV');
		div.innerHTML = '<input id="file' + counter + '" name = "file' + counter + '" type="file" /><input id="Button' + counter + '" type="button" value="Remove" onclick = "RemoveFileUpload(this)" />';
		document.getElementById("FileUploadContainer").appendChild(div);
		counter++;
	}

	function RemoveFileUpload(obj)
	{
		return false;
	}
	
	</script>
</head>
<body onload="AddFileUpload();">
	<div id="FileUploadContainer"></div>
	<input type="button" value="Add" onclick="AddFileUpload();return false;">
</body>
</html></html>

If you use jquery you can use .ready() event to call this whem dom is ready...
HTML
<html>
<head>
	<script src="http://code.jquery.com/jquery-latest.js"></script>
	<script>
		
	var counter = 0;

	function AddFileUpload()
	{
		var div = document.createElement('DIV');
		div.innerHTML = '<input id="file' + counter + '" name = "file' + counter + '" type="file" /><input id="Button' + counter + '" type="button" value="Remove" onclick = "RemoveFileUpload(this)" />';
		document.getElementById("FileUploadContainer").appendChild(div);
		counter++;
	}

	function RemoveFileUpload(obj)
	{
		return false;
	}
	
	$(document).ready(function () {
		AddFileUpload();
	});
	
	</script>
</head>
<body>
	<div id="FileUploadContainer"></div>
	<input type="button" value="Add" onclick="AddFileUpload();return false;">
</body>
</html></html>
 
Share this answer
 
Comments
cutexxbaby 2-Feb-12 7:34am    
thanks
just use onClick=javascriptmethod(); Dude it will call the javascript
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900