0

How would I fire a button click event when a particular button is pressed (in this case the accept button).

I've tried the following but with little success:

Javascript

$('.notification-expand .Request .active-item > .accept-button').click(function () {
    alert("hello");
});

HTML

<div class="notification-expand Request active-item" style="display: block;">
    <div class="notification-body"></div>
    <br>
    <p>
        <button type="button" class="btn btn-success accept-button btn-sm">Accept</button>
    </p>
    <div class="row">
        <div class="col-xs-6 expand-col">
            <button type="button" class="btn btn-warning barter-button btn-sm">Barter</button>
        </div>
        <div class="col-xs-6 expand-col">
            <button type="button" class="btn btn-danger reject-button btn-sm">Reject</button>
        </div>
    </div>
</div>

Fiddle here

5 Answers 5

4

You have error in your selector , it should look like this:

$('.notification-expand.Request.active-item .accept-button').click(function () {
    alert("hello");
});
Sign up to request clarification or add additional context in comments.

1 Comment

Under the same logic would it be possible to do this: $('.notification-expand.Request.active-item .accept-button, .notification-expand.Reject.active-item .accept-button').click(function() { alert("hello"); });
2

You need to concatenate all classes without spaces to catch your target button

$('button.accept-button', '.notification-expand.Request.active-item').click(function () {
    alert("hello");
});

See the updated snippet

Notice the syntax of ".className1.className2" instead of ".className1 .className2"

4 Comments

If I wanted to reference button.accept-button in .notification-expand.Reject.active-item, how would I go about doing it? Can it be done in the same line?
You can use event delegation, to bind click event on .notification-expand.Reject.active-item element, and catch only the target one marked with .accept-button class updated snippet
Thanks. The snippet posted doesn't show it however.
Sorry, I saw Reject and Request the same word :) Below is the updated one snippet
1

should be something like:

$('button.accept-button').click(function(){ ... });

there is really no need to go down the whole list if this is the whole code

----edit----

so when there are more items but only 1 active(i guess) then just target the active-item class:

$('div.active-item button.accept-button').click(function(){ ... });

2 Comments

There are other elements, so it is necessary to tell apart the .active item from the others...
Why not give that button an id?
1

try

$('.accept-button', $('.notification-expand.active-item')).click(function () {
    alert("hello");
});

or

$('.notification-expand.active-item')).find('.accept-button').click(function () {
    alert("hello");
});

Comments

1

Just give the button an id and reference back to that.

HTML

<button id="btnSubmitData"> Ok Button </button>

JQuery Code

$('#btnSubmitData').click(function(){ ... });

You can also have multiple button Ids bind to the same event:

$('#btnAccept, #btnReject, #btnWarning').click(function () {
        alert("hello");
    });

Take a look at the updated Working Fiddle.

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.