0

I have an input type file element:

<input type="file" name="val1" />

And jquery:

$("input[name='val1']").off("click");

But the above JS script (already included in $(function() { }); block) doesn't work, means I can click on Browse... button. I want to disable it or disable click event on this input element.

What's the mistake with my jquery code ? Or it is not possible ?

Thanks

3 Answers 3

4

off removes an event handler. What you're talking about doing is preventing the default action of a click on the file input, which is a different thing.

Either disable the input or, if you want it enabled but non-responsive for some reason, you can prevent the default action of the click using on and false:

$("input[name='val1']").on("click", false);

Gratuitous example | source

From the on docs:

handler(EventObject): The value false is also allowed as a shorthand for a function that simply does return false.

...and of course return false from a handler prevents the default and stops propagation.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use the following to disable the element.

$("input[name='val1']").prop("disabled", true);​

Example on jsfiddle

Comments

2

Just disable the input.

$("input[name='val1']").prop("disabled", true);

http://jsfiddle.net/mattball/DwBTD/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.