Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get button name in angular

I'm trying to get the name of a button selected in a variable, and do something after (change the content of a <p>).

<form>
    <button name="paypal" class="moyen-payement-button" ng-class="{actif: actifSelected.selectedButton === 1}" ng-click="actifSelected.select(1)">
        <img src="images/payement-methods/paypal.svg" alt="Paypal" class="paypal" />
    </button>
    <button name="creditCard" class="moyen-payement-button" ng-class="{actif: actifSelected.selectedButton === 2}" ng-click="actifSelected.select(2)">
         <img src="images/payement-methods/credit-card.svg" alt="Carte de crédit" class="creditCard" />
    </button>
</form>

<p>Name of my button: </p>
<p>1</p>

The <p> with "1" have to change in "2" if the button creditCard is selected, I've find ng-show / ng-hide but I think I can simply modify the content of a <p> without creating another <p>?

like image 301
GuillaumeRZ Avatar asked Feb 08 '23 16:02

GuillaumeRZ


1 Answers

You can use:

<button ng-click="actifSelected.select($event,1)"></button>

And in your controller, you can use $event to trigger element

$scope.actifSelected.select = function($event, value) {
    var button = angular.element($event.currentTarget);
    var buttonName = button.attr("name");
}
like image 186
Kelvin Avatar answered Feb 11 '23 01:02

Kelvin