I have these buttons, which I generate according to the number of records (that I bring from a database) with an ng-repeat
AngularJS button, and I want them to turn green when I put the mouse over them (like the success
bootstrap button) and that in instead of "Lost" say "Found". How could I do that with jQuery? I have already tried several other codes to see if they worked for me and they did not work.
And here I leave the part of the index.html where I generate the buttons:
<!-- MAIN CONTENT -->
<div id="mainContent" class="container-fluid">
<!-- aca hay 2 section más antes de la lista -->
<!-- LIST OF MISSING DOGS -->
<section id="view" class="row row-content" ng-controller="MainController">
<div class="col-xs-offset-3 col-xs-7 well">
<div class="col-xs-offset-3 col-xs-9">
<h1>View the list of Missing Dogs</h1>
</div>
<div class="col-xs-offset-2 col-xs-10">
<div class="form-group">
<label for="race" class="control-label col-xs-11 col-xs-offset-1">Search by race: </label>
<div class="col-xs-8 col-xs-offset-1">
<input class="form-control" name="race" type="text" required placeholder="Race of the dogs you are looking for" ng-model="query"/>
</div>
</div>
<div class="col-xs-10">
<table class="table table-striped table-responsive" ng-show="mascotas.length > 0 && mascotas != null">
<thead>
<tr>
<th>Name</th>
<th>Race</th>
<th>Description</th>
<th>Place</th>
<th>¿Found?</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="mascota in mascotas | filter: {raza : query}">
<td>{{ mascota.nombre }}</td>
<td>{{ mascota.raza }}</td>
<td>{{ mascota.descripcion }}</td>
<td>{{ mascota.lugar }}</td>
<td><button class="btn btn-danger btn-block" ng-click="encontrado(mascota.id)"><span>Lost</span></button></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</section>
</div>
EDIT: I add the code that didn't work for me, I think it's in JavaScript.
$(document).ready(function() {
$('.btn').hover(function() {
$(this).html('Found');
}, function() {
$(this).html('Lost');
});
});
There are many ways to do that. I propose a dynamic alternative with
data-hover
. So, just specify the text you want to display in thehover
.jQuery
AngularJS and CSS
Since you're using Angular, you can make use of
ng-mouseover
andng-mouseleave
to assign a class to it in these states. Likewise, to change the text you only need attributesdata-
and reference them through the CSS propertycontent
.I think this should work when overlaying the pointer:
This works when exiting the pointer:
The first thing I do is edit the
css
delbutton
and then change the name of thespan
.I leave you an example working with
jQuery
:To achieve what you want to do, you can use the
hover()
jQuery function to capture the events when you mouse over the button and when you leave the button, what you have to do is generate a unique id for each button.As you can see I have added an id to the button in this case
You could make a unique id with the pet id and do something similar.
You already have a few answers with JavaScript/jQuery, I'm going to give you one that only needs CSS without the need for anything else.
The idea is to use
::after
(o::before
) to change the content of the button's text, and apply the colors from the .btn-success class when the mouse (:hover
) is hovered over the button:Detailed explanation ( as requested in the comments )
::after
is a pseudo-element that would correspond to a last virtual child that had the element to which it is added. Y is used to add content to the end of the selected element. The value you put in the propertycontent
will be the text that is displayed.What this solution does is it removes the "Lost" and "Found" text in the HTML and then adds it via the pseudo-element
::after
. In normal state the text will be "Lost":Now when the mouse hovers over the button we want the text to change to "Found". For that we use the pseudo-class
:hover
, which is presented when the user puts the mouse over the element. In this case, when the user puts the mouse over the buttonbtn-danger
we want the::after
value to change, so that is obtained by doing.btn-danger:hover span::after
:Finally, we also want to change the color of the button when the mouse is clicked on it and for that we also use the one already mentioned
:hover
(I have obtained the correct colors from the Bootstrap CSS):I hope I have explained it well. Tell me if you need help understanding something.