Reputation: 5977
I am trying to find the way of binding event to an element. Can we bind a JQuery without mentioning function?
I need to make Loan binding enable on click so that it can calls same radio button.
It's getting unbind successfully but could not be rebind to call the same click method in which it is defined.
My code :
$('input[type="radio"]').click(function(){
var radioButId = $(this).attr('id');
var value = $(this).val();
alert(value + " is clicked");
if(value === "home")
{
alert("Enable Loan Link")
$( "#ba").bind( "click" );
}
if(value === "dog")
{
alert("Disable Loan Link");
$( "#ba").unbind( "click" );
}
});
Could someone please help.
Upvotes: 0
Views: 101
Reputation: 26370
You can simply enable and disable "Loan" with the disabled
attribute.
I have updated your fiddle
function fHardCodedFunction(){
alert("bindee");
}
$('input[type="radio"]').click(function(){
var radioButId = $(this).attr('id');
var value = $(this).val();
console.log(value + " is clicked");
if(value === "home")
{
console.log("Enable Loan Link")
$("#ba").removeAttr("disabled");
}
if(value === "dog")
{
console.log("Disable Loan Link");
$( "#ba").attr( "disabled","disabled" );
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
When Dog clicked, Load should disable and when home clicked, Load should be enable <br>
<input type="radio" class="p27" id ="ba" name="ins_type1" value="loan" checked="checked" /> Loan<br><br>
Click here:
<input type="radio" class="p27" id ="ba2" name="ins_type2" value="home" /> Home
<input type="radio" class="p27" id ="ba3" name="ins_type2" value="dog" />
Dog
Notice that when the disabled
attribute is present, clicking the button does not do anything. Besides, it visually greys out the button.
I have also used console.log
, because debugging with alert
sucks :)
Upvotes: 1
Reputation: 30975
A possible way
Fiddle : https://jsfiddle.net/uhxpmexe/7/
JS :
function fHardCodedFunction(){
alert("bindee");
}
function myfunction () {
var radioButId = $(this).attr('id');
var value = $(this).val();
alert(value + " is clicked");
$( "#ba").unbind( "click" );
if(value === "home")
{
alert("Enable Loan Link")
$( "#ba").on( "click", myfunction );
}
if(value === "dog")
{
alert("Disable Loan Link");
}
}
$('input[type="radio"]').click(myfunction);
Upvotes: 3
Reputation: 193
use on and off instead of bind and unbind
check the following link
Upvotes: 0