Jump to content

JavaScript not working in Form

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
I have a button inside of a form. The button's onclick even points to a Javascript function, however I'm getting some strange results. Note that I am not a very experienced Javascript developer.
<html>

<head>

	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<script type="text/javascript">

		function addFile()

		{

			var newFile = document.createElement("input")

			newFile.setAttribute("name", "userfile[]")

			newFile.setAttribute("type", "file")

			

			var newBreak = document.createElement("br")

			

			document.getElementById("filelist").appendChild(newFile)

			document.getElementById("filelist").appendChild(newBreak)

		}

	</script>

</head>

<body>

	<form action="" method="post" enctype="multipart/form-data">

		<div class="block">

			<div class="blockheader">Share these files...</div>

			<div class="blockbody">

				<div id="filelist">

					<input name="userfile[]" type="file" /><br />

				</div>

                                <hr /><input name="addFile" type="button" value="Add File" onclick="addFile()" />

			</div>

			<div class="blockfooter">

				<input type="submit" value="Send files" />

			</div>

		</div>

	</form>

</body>

</html>
The above code does not work. The error is that the function addFile() does not exist. Now when I move the button outside of the form it works. As in the following code.
<html>

<head>

	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<script type="text/javascript">

		function addFile()

		{

			var newFile = document.createElement("input")

			newFile.setAttribute("name", "userfile[]")

			newFile.setAttribute("type", "file")

			

			var newBreak = document.createElement("br")

			

			document.getElementById("filelist").appendChild(newFile)

			document.getElementById("filelist").appendChild(newBreak)

		}

	</script>

</head>

<body>

        <input name="addFile" type="button" value="Add File" onclick="addFile()" />

	<form action="" method="post" enctype="multipart/form-data">

		<div class="block">

			<div class="blockheader">Share these files...</div>

			<div class="blockbody">

				<div id="filelist">

					<input name="userfile[]" type="file" /><br />

				</div>

			</div>

			<div class="blockfooter">

				<input type="submit" value="Send files" />

			</div>

		</div>

	</form>

</body>

</html>
The above code works perfectly. Could anyone explain why this is, and hopefully a way to fix it.

Thanks for reading.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Why do you have it as an input? What happens if it's just a piece of text or an image or something?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
I found out what was wrong. I'm not really sure how Javascript treats elements on the page but there was a naming conflict between the button and the addFile() function, both were named addFile. Once I changed the input's name to addFileBtn it worked fine.