Paul Stevens Paul Stevens - 6 months ago 12
HTML Question

Remove unwanted tags from clipboard

I have a function which is successfully returning a ul list of random numbers between 1 and 100, and it places the numbers on the user's clipboard. This is just for IE but this isn't a problem.
This issue is that the resulting text on the clipboard is retaining the li tags like below. Is there a way to remove these tags from the data before it hits the clipboard?

<LI>68</LI>
<LI>97</LI>
<LI>94</LI>
<LI>43</LI>
<LI>45</LI>
<LI>65</LI>


The JS code:

function ReturnRandomNumbers() {
var howManyInt = parseInt(document.getElementById("txtMultiples").value);
var listNumbers = document.getElementById("listNumbers");
var i = 0;

if (howManyInt < 101 && howManyInt > 0) {
listNumbers.innerHTML = "";
while (i < howManyInt) {
AddItemToList(randomNumber());
i++;
}

window.clipboardData.setData("text",listNumbers.innerHTML);
alert(listNumbers.innerHTML);
} else {
alert("Value must be between 1 and 100");
}
}


function AddItemToList(item) {
var ol = document.getElementById("listNumbers");
var li = document.createElement("li");
li.appendChild(document.createTextNode(item));
ol.appendChild(li);
}

Answer
var str = "<LI>68</LI>\n<LI>97</LI>\n<LI>94</LI>\n<LI>43</LI>\n<LI>45</LI>\n<LI>65</LI>"

or

var str = listNumbers.innerHTML;

You want to strip the li tags from the string, you can do a regex match and replace

var stripped = str.replace(/</*LI>/g,'');

Which will give

"68 97 94 43 45 65"

Pass it to your clipboard window.clipboardData.setData("text",stripped);

Comments