jquery - Store original text once -
i have simple function gets original text of div, , changes text when user resizes window (responsive site). when user enlarges window again, original text must shown. it's simple, have small problem.
function limith2text() { var origtext = $(".result .right .mask").text(); var width = $(window).width(); if (width < 811) { $(".result .right .mask").each(function () { var $this = $(this); $this.text($this.text().slice(0, 75)); $this.append('...').show(); }); } else { $(".result .right .mask").each(function () { $(this).text(origtext); }); } } the console shows var origtext contains text of 3 [.result .right .mask] divs. see, html repetition of [.result .right .mask]. instead of (for example) 'hello text', shows 'hello text hello text hello text'. when user resizes, grows exponential browser crashes. can't figure out how once store text of 1 div?
thanks help!
try using .eq():
var origtext = $(".result .right .mask").eq(0).text(); this reduce set of matched elements 1 @ specified index , store text of 1 div.
Comments
Post a Comment