(function(str) { var d = document.querySelectorAll('div'); d = [].filter.call(d, function(elem, indx) { return elem.firstChild && elem.firstChild.textContent == str; })[0]; alert(d.textContent); })('Спам');