0

I am trying to find the way of binding event to an element. Can we bind a JQuery without mentioning function?

Here is my fiddle

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.

4
  • See this: api.jquery.com/trigger Commented May 13, 2015 at 9:45
  • There's a reason why you can't post a JSFiddle link without code in your question: Please add the relevant / minimalistic code in your question itself! Outside links can be useful, but they may break after some time, and then your question will be useless for any future readers. Make sure to add all the necessary info in your question itself. Commented May 13, 2015 at 9:48
  • Please put code in your question. We should not have to go to another site to look for it. Questions should standalone, and use demos as backup. If you want help, you need to help us help you Commented May 13, 2015 at 9:49
  • I keep wondering why on Earth you absolutely want to write javascript without using functions. It's like "I want to drive a car, but I don't want to use the steering wheel". Commented May 13, 2015 at 11:28

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Is myfunction required? Can't we do without it as asked in question title?
@fatherazrael Yes, it is required in this way. Otherwhise how will it know what to do when rebinding the event?
@Yenne Info Thanks. I implemented this in my solution
1

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 :)

4 Comments

i must not use disabled as it is requirement
Well you didn't mention any requirement in your question.
Can we bind a JQuery without mentioning function? <- This was my question
Yep, and where in that is "Another requirement is, I can't use disabled" ? :)
0

use on and off instead of bind and unbind

check the following link

https://api.jquery.com/off/

1 Comment

Please add the relevant code in your answer itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.