0
$('img').click(function () {
    $("#shikh_sec").attr("disabled", true); // doesn't work :/
    }

so , how to fix this and disable that element when clicking on the "img" tag

Ahmed Mohsen
  • 114
  • 2
  • 9

4 Answers4

3

Assuming #shikh_sec is an input that can be disabled (there's no such thing as a disabled p element, etc.), you want prop():

$('#shikh_sec').prop('disabled', true);
Paul Roub
  • 35,100
  • 27
  • 72
  • 83
  • 1
    Note that this is limited to jQuery 1.6+ –  Sep 30 '14 at 21:45
  • Hard to guess why not without knowing what the element *is*. Or if you might mean something different by 'disabled'. Or if the initial code is being run before the image in question has been added to the page. If you'd include sample code that actually shows the problem, we'd be better able to help. – Paul Roub Sep 30 '14 at 21:48
  • For example, it works here: http://codepen.io/paulroub/pen/qmFoJ (having added the missing `)` that someone pointed out). – Paul Roub Sep 30 '14 at 21:49
1

The code is missing a trailing ");"

A correct version would be something like this

$('#disable-me').click(function () {
$(this).attr("disabled", true); // doesn't work :/
});

JSFiddle: http://jsfiddle.net/5v4mysgt/

Ted Plaisted
  • 121
  • 5
  • 1
    The disabled attribute does not accept true as a value, you have to give it 'disabled' or use the prop setter if using jQuery 1.6+ –  Sep 30 '14 at 21:48
  • It seems to be working for me, although it's proper to use $(this).attr("disabled", "disabled") – Ted Plaisted Sep 30 '14 at 21:53
0

Try using disabled instead of true:

$('img').click(function () {
    $("#shikh_sec").attr("disabled", "disabled");
}

http://jsfiddle.net/yhfnzjuq/

$(function(){
  $('img').click(function () {
    $("#shikh_sec").attr("disabled", "disabled");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="http://ichef.bbci.co.uk/news/ws/200/amz/worldservice/live/assets/images/2014/09/23/140923115528_ultima_hora_640x360_bbc_nocredit.jpg">
    
<input id="shikh_sec" type="button" value="OK">
Ragnar
  • 3,773
  • 1
  • 22
  • 38
-2

Or just with a pure javascript:

$('img').click(function () {
    document.getElementById("shikh_sec").disabled = true;
});
undefined
  • 136,817
  • 15
  • 158
  • 186
Goran.it
  • 5,001
  • 2
  • 19
  • 25
  • Downvoter .. you could leave some comment prior to downvoting the answer .. I hate haters – Goran.it Sep 30 '14 at 21:46
  • I would guess that the DV'er wanted to use the default reason for down-voting. It doesn't indicate hate. Having said that, you're not using "pure JavaScript". – Jay Blanchard Sep 30 '14 at 21:50
  • Probably the use of `$('img')` in something labeled as "pure javascript". If you're using jQuery, why not keep using jQuery in the following line? – Paul Roub Sep 30 '14 at 21:50
  • you solved it , thanq – Ahmed Mohsen Sep 30 '14 at 21:51
  • 1
    This is just an alternative, if it doesn't deserve upvote it doesn't deserve downvote either, after all it works! – undefined Sep 30 '14 at 21:52
  • Ok guys, read the question again please .. it's not about how to bind an event but how to disable some tag. – Goran.it Sep 30 '14 at 21:53
  • Yes @Goran.it, but it's not the best way when you are working with jQuery. – Ragnar Sep 30 '14 at 22:07
  • @Ragnar And using `attr` for modifying boolean attributes is not the best way when you are working with jQuery! – undefined Sep 30 '14 at 22:09
  • @undefined `attr("disabled", "disabled")` works for all jQuery versions. – Ragnar Sep 30 '14 at 22:16
  • @Ragnar Please check this question as an example http://stackoverflow.com/questions/6048022/jquery-attrdisabled-disabled-not-working-in-chrome – undefined Sep 30 '14 at 22:20