Sike12
Sike12

Reputation: 1262

window.event javascript code does not work in Firefox

I have an markup

            <div id="uploadControl" class="fileUpload1">
                    <label for="uploadFile" id="labelId">Choose File</label>
                    <input class="upload" type="file" id="uploadFile" />
                </div>

javascript

        window.onload = function () {
    document.getElementById('uploadControl').onclick = function (event) {

        // process the event only for the original source 
        if (window.event.srcElement.id === 'uploadControl') {
            document.getElementById('labelId').click();
        }
        //prevent event propagation
        window.event.cancelBubble = true;
    };
}

I want to the label to fire a click event for the input File whenever the "upload control" div is clicked. The problem is.... this works on IE and chrome but on firefox i get the message
"TypeError: window.event is undefined "
It seems like firefox doesnt support window.event code. How can i make it work on firefox? please help

Upvotes: 0

Views: 2400

Answers (2)

Rahul Sharma
Rahul Sharma

Reputation: 453

Firefox doesn't support windows.event object. Use these variables to store the value and it will work after that use your code:

var event = e || window.event
var assumed = (event.target || event.srcElement).id;

Upvotes: 1

Dgan
Dgan

Reputation: 10285

try this:

Tested and 100% working

You can Combine here event and this(element)

function postBackByObject(e,d) {
            var target = e.target || e.srcElement;     // Support IE6-8

            if (d.id == 'uploadControl') {

                document.getElementById('labelId').click();
            }
            target.cancelBubble = true;
        }


<div id="uploadControl" class="fileUpload1" 
 onclick="postBackByObject(event,this);">
<label for="uploadFile" id="labelId">Choose File</label>
<input class="upload" type="file" id="uploadFile" />
                </div>

Upvotes: 0

Related Questions