I am trying to get strings from html body that has elements "superscript and subscript" tags. So far i am able to get these tags. But i also want to have an option to find strings/characters that don't have the sup/sub tags. How can i find strings with no tags attached. Please help. My code below so far:
function myFunction() {
var e = document.getElementById("t1");
var strUser = e.options[e.selectedIndex].value;
var i;
for (i = 0; i < strUser.length; i++) {
var list = document.getElementsByTagName("SUP");
}
var y;
for (y = 0; y < list.length; y++) {
if(list[y].innerHTML.indexOf(strUser) !== -1) {
$(list[y]).addClass("test");
}
}
}
function myFunctionr() {
var e = document.getElementById("t1");
var strUser = e.options[e.selectedIndex].value;
var i;
for (i = 0; i < strUser.length; i++) {
var list = document.getElementsByTagName("SUB");
}
var y;
for (y = 0; y < list.length; y++) {
if(list[y].innerHTML.indexOf(strUser) !== -1) {
$(list[y]).addClass("test");
}
}
}
.test {
/*border: 3px inset red;*/
background: red;
color: white
}
#mydiv{
background: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<sup>®</sup>
<sup>®</sup>
<sup>®</sup>
<sub>®</sub>
<sub>®</sub>
<sub>®</sub>
<sub>™</sub>
<sup>™</sup>
<sup>™</sup>
®
</div>
<select id="t1">
<option value="™">™</option>
<option value="®" >®</option>
</select></br></br>
<button onclick="myFunction()">Search Superscript</button>
<button onclick="myFunctionr()">Search Subscript</button>
Document.querySelector()for more powerful selectors. However, what are you trying to do, generally? I feel like there has to be a better strategy...