innertext

Javascript .querySelector find <div> by innerTEXT

醉酒当歌 提交于 2019-11-26 17:27:57
How can I find DIV with certain text? For example: <div> SomeText, text continues. </div> Trying to use something like this: var text = document.querySelector('div[SomeText*]').innerTEXT; alert(text); But ofcourse it will not work. How can I do it? gdyrrahitis OP's question is about plain JavaScript and not jQuery . Although there are plenty of answers and I like @Pawan Nogariya answer, please check this alternative out. You can use XPATH in JavaScript. More info on the MDN article here . The document.evaluate() method evaluates an XPATH query/expression. So you can pass XPATH expressions

Cross-browser innerText for setting values

我的未来我决定 提交于 2019-11-26 16:54:22
问题 Let's say I have the following code: <html> <head></head> <body> <div id="d">some text</div> <script type="text/javascript"> var d = document.getElementByid('d'); var innerText = d.innerText || d.textContent; innerText = 'new text'; </script> </body> </html> And I want to change text value for the div tag with id='d'. Unfortunately the block code above doesn't work and the text content doesn't change. It works if do the following recipe: if (d.innerText) d.innerText = 'new text'; else d

Javascript .querySelector find <div> by innerTEXT

谁都会走 提交于 2019-11-26 05:24:28
问题 How can I find DIV with certain text? For example: <div> SomeText, text continues. </div> Trying to use something like this: var text = document.querySelector(\'div[SomeText*]\').innerTEXT; alert(text); But ofcourse it will not work. How can I do it? 回答1: OP's question is about plain JavaScript and not jQuery . Although there are plenty of answers and I like @Pawan Nogariya answer, please check this alternative out. You can use XPATH in JavaScript. More info on the MDN article here. The

How to get element by innerText

喜欢而已 提交于 2019-11-26 04:03:25
How to get tag in html page, if I know what text tag contains. E.g.: <a ...>SearchingText</a> You'll have to traverse by hand. var aTags = document.getElementsByTagName("a"); var searchText = "SearchingText"; var found; for (var i = 0; i < aTags.length; i++) { if (aTags[i].textContent == searchText) { found = aTags[i]; break; } } // Use `found`. You could use xpath to accomplish this var xpath = "//a[text()='SearchingText']"; var matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; You can also search of an element containing

How to get element by innerText

旧街凉风 提交于 2019-11-26 01:43:46
问题 How to get tag in html page, if I know what text tag contains. E.g.: <a ...>SearchingText</a> 回答1: You'll have to traverse by hand. var aTags = document.getElementsByTagName("a"); var searchText = "SearchingText"; var found; for (var i = 0; i < aTags.length; i++) { if (aTags[i].textContent == searchText) { found = aTags[i]; break; } } // Use `found`. 回答2: You could use xpath to accomplish this var xpath = "//a[text()='SearchingText']"; var matchingElement = document.evaluate(xpath, document,