Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the correct way to disable an input type="image" using jquery?

Tags:

jquery

forms

With jQuery, I'm trying to disable an input field like this:

<input id="submit" type="image" src="submit.jpg">

What I would like to do is disabling the button and change the image with a different image (submitGreyed.jpg) to visually notify that button is disabled.

With the following line I disable the button:

JQuery("#submit").attr('disabled','true');

then I change the image with:

JQuery("#submit").attr('src','submitGreyed.jpg');

and once disabled I submit the form with:

JQuery("#form").submit();

The second line has some weird behaviour; sometimes works and sometimes doesn't.

When it works, button is disabled, image changed and form is submitted; when it does not work, button is disabled, form is submitted but image is not changed.

How can I solve this?

like image 308
systempuntoout Avatar asked Feb 06 '10 01:02

systempuntoout


People also ask

How do you disable right click on images jQuery?

Disable right click menu in html page using jquery. JavaScript Code: $(document). bind("contextmenu",function(e){ return false; });

How do you disable an image in HTML?

One way to disable dragging an image from an HTML page is to set the ondragstart property of an image element to a function that returns false .

How do I disable all inputs in a form?

Answer: Use the jQuery prop() method You can use the jQuery prop() method in combination with the :input selector to disable all <input> , <textarea> , <select> and <button> elements inside an HTML form dynamically using jQuery.

How do I disable a button in jQuery?

To disable a button with jQuery you need to set the disabled property on the button using the prop method. For example $('. my-button'). prop('disabled', true) .


1 Answers

This doesn't answer your question exactly, but I hope it helps:

First, it should be disabled="disabled" so use this:

jQuery("#submit").attr('disabled','disabled');

And I am not sure what your grayed out button looks like, but you could try just using opacity:

jQuery("#submit").attr('disabled','disabled').css('opacity',0.5);

Update I couldn't replicate the problem, so here is my suggestion:

Use an absolute path to the image instead of a relative one, and set both attributes at the same time (Though setting one after the other didn't change my test):

jQuery("#submit").attr({
   disabled: 'disabled',
   src:      '/images/submitGreyed.jpg'
});

Since in my test I used a full path, that might have affect it a bit.

View a demo here

like image 139
Doug Neiner Avatar answered Oct 13 '22 01:10

Doug Neiner