0

I have the following code:

echo "<button type=\"button\" class=\"btn btn-link\" onclick=\"personchange('{$row['first_name']}')\">{$row['first_name']}</button>";

What I am trying to do is read all the names from the database and create a button for each name. When a user clicks on a name, I want to call the personchange Javascript function, which works, and add a class to the button that changes the way it looks (so that the user knows which name is currently selected). I know we have to use jQuery to add the class, but how do I know which button I selected in jQuery?

3 Answers 3

2

You can add a class to button on click like this:

$('.btn').click(function() {
    $(this).addClass('clicked');
});

Is that what you're looking for?

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

2 Comments

I'm assuming that OP wants one name selected at a time, so there should also be an extra line: a .removeClass('clicked') on the currently selected button before the addClass is called ;)
@Nondeterministicnarwhal They would have to add $('.btn').removeClass('clicked'); to the function passed to .click().
0

You can have an id attribute on each button, and in the onclick() function you may add that id as a parameter. You can then access the button by its id.

Comments

0

Since we're doing icky-inline JavaScript:

onclick="personchange(...); this.className += ' clicked';"

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.