Is it possible to assign a class to elements like a
and span
in the scenarios below:
$('<a/>').click(function(){...})
$("<span/>").text(chars.substr(0, limit-1));
=================
Below is the complete script. It involves adding spans and links to specific text segments. I want to include classes on the spans or links for custom styling. The code needs to be compatible with jquery 1.3x:
var limit = 200,
chars = $("#showhideDiv").text();
if (chars.length > limit) {
var visiblePart = $("<span/>").text(chars.substr(0, limit-1));
var hiddenPart = $("<span/>").text(chars.substr(limit-1)).hide();
var readMore = $('<a/>').click(function() {
hiddenPart.toggle();
readMore.toggle();
readLess.toggle();
return false;
}).text ("Show more")
var readLess = $('<a/>').click(function() {
hiddenPart.toggle();
readMore.toggle();
readLess.toggle();
return false;
}).text("Show less").hide();
$("#showhideDiv").empty()
.append(visiblePart)
.append(readMore)
.append(hiddenPart)
.append(readLess);
}