2

I am setup right now to use a div tag to trigger a hidden radio button. However, when I click once on the div, the radio button switches, but it I must double click the div in order for the data to be sent via ajax. Any ideas on what is causing this and how to fix it?

jQuery

$(function(){
    $(".albumcover_tag").click(function(e){
        e.stopPropagation();
        $('.edit_album').trigger('submit.rails');
    });
    $(".albumcover-item").click(function(e){
        $('.albumcover-item').removeClass("radio-selected");
        $(this).addClass("radio-selected");
        $(this).find('input').click();
        return false;
    });
    $('.albumcover_tag:checked').parent('.albumcover-item').addClass("radio-selected");
});

HTML

<div class="albumcover-item">
    <label for="album_albumcover">Set as Album Cover </label>
    <input class="albumcover_tag" id="album_albumcover_id_2" name="album[albumcover_id]" type="radio" value="2">
</div>

NOTE: I want to be able to enable the ajax on single click, not double click.

derek_duncan
  • 1,377
  • 1
  • 13
  • 22

3 Answers3

0

you wil find answer here, you have to cancel single click when triggering doubleclick.

Need to cancel click/mouseup events when double-click event detected

function singleClick(e) {
    // do something, "this" will be the DOM element
}

function doubleClick(e) {
    // do something, "this" will be the DOM element
}

$(selector).click(function(e) {
    var that = this;
    setTimeout(function() {
        var dblclick = parseInt($(that).data('double'), 10);
        if (dblclick > 0) {
            $(that).data('double', dblclick-1);
        } else {
            singleClick.call(that, e);
        }
    }, 300);
}).dblclick(function(e) {
    $(this).data('double', 2);
    doubleClick.call(this, e);
});
Community
  • 1
  • 1
Davor Mlinaric
  • 1,989
  • 1
  • 19
  • 26
0

Try this:

$(function(){
    $(".albumcover-item").click(function(e){
        $('.albumcover-item').removeClass("radio-selected");
        $(this).addClass("radio-selected");
        $('.edit_album').trigger('submit.rails');
    });
});
vher2
  • 980
  • 6
  • 9
0

I fixed it by moving the ajax request to the div element's click() trigger.

$(".albumcover-item").click(function(e){
    ...
    $('.edit_album').trigger('submit.rails');
    ...
});
derek_duncan
  • 1,377
  • 1
  • 13
  • 22