I am very new to jquery and haven't been able to find a solution for my problem. I have a table created for some messages. At the beginning of a messages row there is a fontawesome
icon showing the message is not expanded. Once clicked and expanded I want that icon to change to a different one. I have been toying with a function found elsewhere on this site which uses the value of the first <td>
in a row to show either +
or -
.
Here's the function followed by my table:
$(function() {
$("td[colspan=7]").find("p").hide();
$("table").click(function(event) {
event.stopPropagation();
var $target = $(event.target);
if ( $target.closest("td").attr("colspan") > 1 ) {
$target.closest("td").children("p").slideUp();
$target.closest("tr").prev().find("td:first").html("+");//NOT MINE - CHANGE
} else {
$target.closest("tr").next().find("p").slideToggle();
if ($target.closest("tr").find("td:first").html() == "+")//NOT MINE - CHANGE
$target.closest("tr").find("td:first").html("-");//NOT MINE - CHANGE
else
$target.closest("tr").find("td:first").html("+"); //NOT MINE - CHANGE
}
});
});
...
echo "<tr class='message-body'>";
echo "<th><i class='far fa-envelope'></i> ".$title."</th>";
echo "<th></th>";
echo "<th>".$date."</th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo '<th><div class="right"><form method="post" action="">
<input type="hidden" name="id" value="'.$id.'">
<button class="rem-button"> Delete</button></form></div></th>';
echo "</tr>";
echo "<tr colspan='7'><td class='message-body-body' colspan='7'><p><br>".$body."</p></td></tr>";
Ideally I want to do something like:
closed -> first <i>
class is = far fa-envelope
opened -> first <i>
class is = far fa-envelope-open
EDIT
Currently the function allows me to click a row and expand to show the body of a message. Before expanding i want to keep the envelope
icon. After expanding I want that icon to change to envelope-open
.
EDIT 2
I need this line
$target.closest("tr").prev().find("td:first").html("+");
changed to this one (but functional)
$target.closest("tr").prev().find("i:first").class.html("far fa-envelope");
the other lines needing to be changed will be, in a similar manner
EDIT 3 solution my question is based off of
EDIT 4 My Fiddle not sure how to link fontawesome here