I have a button that is showing more or less of a content block in search results. When I click “More” it expands as needed, but nothing happens when I click “Less”. I feel like it has to do with my preventDefault(e) line. Code is below…
$(".btnMore").click(function (e) {
e.preventDefault(e);
if ($(this).attr('value', 'More')) {
$(this).parent().prev().switchClass("abstractText", "abstractTextNoHeight", 500, "swing");
$(this).attr('value', 'Less');
}
else if ($(this).attr('value', 'Less')) {
$(this).parent().prev().switchClass("abstractTextNoHeight", "abstractText", 500, "swing");
$(this).attr('value', 'More');
}
});
That is the wrong way of using “attr()” to check for attribute value:
Will set the value of “value” attribute to “More” and return
undefined. What you need to do is this:or
There is no need to use jQuery when JavaScript is sufficient. Notice the lack of
$()aroundthis– since you’re using a JS function (getAttribute), you don’t need to create a jQuery collection, which makes your code faster and simpler.