0
   <a href="<?php print $base_url ?>/node/<?php print $part;?>/take"  id="take_quiz1" >
<button  class="btn btn-white btn-cons btn-cancel"  >Take quiz test</button>
</a>

Here i am using both anchor tag and button tag.How to disable this menu in jquery?

Dhivya
  • 341
  • 1
  • 3
  • 10
  • possible duplicate of [jQuery disable all button of a css class](http://stackoverflow.com/questions/3769223/jquery-disable-all-button-of-a-css-class) – Pratik Joshi Apr 18 '14 at 10:34

4 Answers4

2

Use

$(".btn-cancel").prop("disabled",true);

Live demo http://jsfiddle.net/wWRx6/

IF link also u have to disable http://jsfiddle.net/wWRx6/2/

 <a href="javascript:void(0)"  id="take_quiz1" >
<button  class="btn btn-white btn-cons btn-cancel"  >Take quiz test</button>
</a>
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
  • @ChrisBrown Because `.prop()` only handles properties but not values, while `.attr()` handles values of the properties. Using `.attr()` on boolean properties (without any values required, actually) may cause conflicting behavior. Plus, you cannot simply set `.attr('disabled', false)`. You can read the documentation on `.prop()` [here](http://api.jquery.com/prop/) – Terry Apr 18 '14 at 10:36
  • 1
    @ChrisBrown for a structured answer, check this [.prop() vs .attr()](http://stackoverflow.com/a/5884994/1671639) – Praveen Apr 18 '14 at 10:37
  • @pratik Josshi but the link still workin..it doesn't get disable – Dhivya Apr 18 '14 at 10:49
  • @PratikJoshi i want to disable the button...and the link should not go to anywhere – Dhivya Apr 18 '14 at 10:51
  • Then u use PHP to set this , – Pratik Joshi Apr 18 '14 at 10:53
  • If i click the button,it goes directly to the link – Dhivya Apr 18 '14 at 11:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50963/discussion-between-dhivya-and-pratik-joshi) – Dhivya Apr 18 '14 at 11:58
1

You can use jQuery prop method to update disabled attribute

$('.btn').prop('disabled', true);

To disable link you can use preventDefault() method

$('#take_quiz1').click(function(e) {
    // Update the flag to true/false as per your requirements 
    if(flag){ 
     e.preventDefault();
    } 
    //...
});
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
0

You disable a button by:

$('button').attr('disabled','');

You add disabled attribute to the button

You do that by .attr

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

try this it should help you

 $(".btn-cons").attr("disabled", "disabled");

here is live demo

http://jsfiddle.net/wWRx6/1/

Sumit Pathak
  • 671
  • 1
  • 6
  • 25
  • PSA: Do not use `.attr()` for boolean attributes. It's a bad idea! – Terry Apr 18 '14 at 10:37
  • @Terry, first of all a boolean isn't a valid value for disabled. – Amit Joki Apr 18 '14 at 10:38
  • No, boolean attributes are called boolean because it's their **presence or absence**, not their declared values), that dictates a certain behavior. [Refer to the docs](http://www.whatwg.org/specs/web-apps/current-work/#boolean-attributes). – Terry Apr 18 '14 at 10:40
  • @Terry i edited my answer with js fiddle it and its work fine then what is the problem ? – Sumit Pathak Apr 18 '14 at 10:45
  • @SmartKiller It works, but that is not good practice. So is using `` or `
    ` in modern browsers. They work, perfectly fine, but not the right way to get stuff done.
    – Terry Apr 18 '14 at 10:47