Cleardragonf Cleardragonf - 2 months ago 7
HTML Question

How to split innherHTML string using Javascript into specific parts after a certain character like a '+' sign

I need to break a string apart after certain characters.

document.getElementById("result").innerHTML = Monster + "<p id='vault" + loop + "'> || HP: " + HP + "</p>" + " || Defense: " + Def + " || Attack: " + ATK + " || Can it Dodge/Block: " + DB + " || Can it retaliate: " + RET + " || Initative: " + INT + " || Exp: " + MEXP + " <input type='submit' class='new' onclick='Combat(" + loop + ")' value='FIGHT!'></input>" + "<br><br>" + A;




function Chest(id){
window.open('LootGen.html', '_blank');
}
function Combat(id){
document.getElementById("C").value = document.getElementById("vault" + id).innerHTML;
}


When this runs the value that results is:


|+HP:+20


However I only want '20' part,now keep in mind that this variable does change and so I need to use substrings to somehow pull that second number after the
+
. I've seen this done with:


var parameters = location.search.substring(1).split("&");


This doesn't work here for some reason as first of all the var is an
innher html
.
Could someone please point me in the write direction as I'm not very good at reading docs.

Answer
var text = "|+HP:+20";
// Break string into an array of strings and grab last element
var results = text.split('+').pop();

References: