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?
Disable right click menu in html page using jquery. JavaScript Code: $(document). bind("contextmenu",function(e){ return false; });
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 .
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.
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) .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With