Maciej Jaśniaczyk
Maciej Jaśniaczyk

Reputation: 605

jQuery - radio button click()

Is it possible to disable standard action being performed while clicking radio button? (without "disable" attr)

So when i click particular radio button, nothing happens. Tried .unbind('click') but doesnt seem to work.

Ty in advance for help

Upvotes: 2

Views: 6818

Answers (6)

Pipalayan Nayak
Pipalayan Nayak

Reputation: 957

Actually, the first time you click the radio, the checked property is set and hence it shows as checked in browser ( for the firtst time ).

So this piece of code will solve the issue

[I am using jquery library]

$('input:radio').click(function(){
$(this).prop('checked',false);
e.preventDefault();
return false;
});

Upvotes: 1

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262919

You can use the preventDefault() method exposed by the event object:

$("input:radio").click(function(e) {
    e.preventDefault();
});

EDIT: Unfortunately, this does not seem to prevent the browser from checking the radio button that is clicked first (jsFiddle is in "emergency read-only" mode, so I cannot post a demo right now).

Upvotes: 4

skillet-thief
skillet-thief

Reputation: 549

You need to use event.disableDefault in the function you give to jQuery click.

 $("#my_element").click(
function(event) {
event.preventDefault();
// the rest of your code goes here
}
);

Upvotes: 0

TeChn4K
TeChn4K

Reputation: 2405

You can try this, this will stop propagation :

$("#my-radio-btn").live("click", function(e){
    e.preventDefault();
});

Upvotes: 1

ShankarSangoli
ShankarSangoli

Reputation: 69905

Just try this.

$('input:radio').click(function(e){
   e.preventDefault();
});

Demo

Upvotes: 1

techfoobar
techfoobar

Reputation: 66663

This should work:

$('#myradio').click(function(e) {
  e.preventDefault();
  return false;
});

Upvotes: 1

Related Questions