0

i have these two buttons and i want one of them to be disabled when the other was clicked.

There is html

    <button type="submit" id="button_user" name="submit" class="btn btn-small btn-primary" data-toggle="collapse" data-target="#fsearch">Pretraga terena na sajtu</button>
  <button type="submit" id="button_field" name="submit" class="btn btn-small btn-primary" data-toggle="collapse" data-target="#usearch">Pretraga korisnika na sajtu</button>

I have managed to do this with two functions and onclick, but i would like to know is there any elegant way to do this in one function so that inline js can be avoided?

this is my js:

$(document).ready(function(){


disable_one = function(){
    if($('button_user').data('clicked', true)){
        $('#button_field').attr("disabled", "disabled");
        return;
    }
};
disable_two = function(){
    if($('button_field').data('clicked', true)){
        $('#button_user').attr("disabled", "disabled");
        return;
    }
};


});
1
  • If you don't absolutely need buttons, use radio elements. Commented Apr 1, 2013 at 21:09

4 Answers 4

2

Using jQuery:

$('.btn.btn-small.btn-primary').click(function () {
    $('.btn.btn-small.btn-primary').not($(this)).prop('disabled', true)
});

jsFiddle example

Sign up to request clarification or add additional context in comments.

Comments

1
$('#button_user').on('click', function() {
    $('#button_field').attr('disabled', 'disabled');
});

Working example: http://jsfiddle.net/BPZT7/

Comments

0

Bind a Jquery click event to the button class and then us the target property with the Jquery is method (http://api.jquery.com/is/).

 $("your button class").click(function(e) {
    if($(e.target).is("#button_user")) {
        //Disable button_field
    } else {
        // Disable button_user
    }
})

Comments

0

This is how i managed to do this very simple and clear. Now, if the visitor change his mind this button is enabled so that he can choose again.

    $(document).ready(function(){

$('#button_user').click(function () {
    if (button_field.disabled==false){
        button_field.disabled = true;
    }
    else {
    button_field.disabled = false ;
    } 
});
$('#button_field').click(function () {
    if (button_user.disabled==false){
        button_user.disabled = true;
    }
    else {
    button_user.disabled = false ;
    } 
});


});

Comments

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.