Hi Guys,
I could really use some help with this now, I have written this from scratch which is pretty good for me and it works which is also pretty good for me, but there are a few technically problems I'm having that I can't figure out.
Firstly I would like to get the same functionality to work in moz too (it only works in ie at the moment - the attachEvent code and .click() aren't moz compatible).
Secondly I was wondering if I'm using 'too much DOM' for a task that could be achieved more simply? Your advice and suggestions would be appreciated, as would the best ways to 'streamline' what I have which I think is a bit on the messy and cumbersome side.
And thirdly I was hoping you could suggest the best way to 'hide' the original file browse field when I create the new one.
In case you were wondering my aim is to produce a script that will scan for a file upload field in a page and switch it so that the 'browse' button can be an image. The point of using the DOM so much was to avoid putting code in the page since if javascript isn't enabled the file upload field still needs to work albeit with an ugly look.
code:
<html>
<head>
<script language="JavaScript" type="text/JavaScript">
function fixUpload() {
input = document.getElementsByTagName('INPUT')
newInput = document.createElement( 'INPUT' )
newInput.type = 'text'
newInput.id = 'newInput'
inputBtn = document.createElement( 'INPUT' )
inputBtn.type = 'image'
inputBtn.name = 'inputBtn'
inputBtn.src = 'myButton.gif'
inputBtn.attachEvent('onclick',function(){form[inputName].click()})
for (i=0;i<input.length;i++) {
if (input[i].type.toLowerCase()=='file') {
inputName=input[i].name
inputObj=document.getElementById('newInput')
form=input[i].parentNode
form.insertBefore(inputBtn,input[i])
form.insertBefore(newInput,inputBtn)
input[i].attachEvent('onchange',function(){inputObj.value = form[inputName].value;inputObj.focus()})
}
}
}
window.onload=fixUpload
</script>
</head>
<body>
<form name="form1" id="form1" enctype="multipart/form-data" method="post" action="">
<input type="file" name="file" />
</form>
</body>
</html>
In hindsight whilst writing this post I have just thought that maybe any easier solution would be to somehow 'hide' the browse button whilst keeping the 'browse' field and just create the image button. Thereby missing out the whole extra text field thing, but I'm not sure that would be possible.
Please help me out,
Jon
visit my CryoKinesis Online Gallery