Reputation: 83
i have written a javascript codefor autosuggest. i am getting autosuggest.but when i am selecting any autosuggested word.it's coming with html tag like this.:
if i am typing "w" in text box. then i am getting some autosuggested word if i select that word then on the text box it coming like w water,wall etc.
below is my code.:
<html>
<head>
</head>
<body>
<input name="txt" type="text" id="new"
placeholder="Please key in some text.">
<script type="text/javascript" src="autoComplt.js"></script>
<script>
var words =[];
function get_words(q,calbk)
{
var xmlhttp = new XMLHttpRequest();
var url = "http://xxxx.poc.yyyyy.com/v1/xxx/suggest?authKey=baef7f8e39c53t6990f852c8a14b7f6018b58&q="
+ q + "&rows=8";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
var items = data.suggestions;
var words = items.map(function(item){return item.suggestion;});
console.log(words);
calbk(words)
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
var input = document.querySelector("input[name=txt]");
autoComplt.enable(input, {
hintsFetcher : function(v, openList) {
console.log("in")
get_words(v,openList)
}
});
</script>
</body>
</html>
how can i remove that html tag from the textbox.
Upvotes: 0
Views: 95
Reputation: 707326
You can convert an HTML string to text by inserting it into a DOM element and then asking for just the text back out. This will work in even older versions of IE:
function stripHTML(html) {
var div = document.createElement("div");
div.innerHTML = html;
return div.textContent || div.innerText;
}
Working demo: http://jsfiddle.net/jfriend00/tbnfnvv6/
Upvotes: 1
Reputation: 136707
You could use the DOMParser API : and look for the textContent of result returned :
var words = ["<b>w</b>hite out", "<b>w</b>ireless mouse", "<b>w</b>hite board", "<b>w</b>ater", "<b>w</b>ireless keyboard", "<b>w</b>ireless router", "<b>w</b>ireless printers", "<b>w</b>indow envelopes"]
var p = new DOMParser().parseFromString(words[Math.floor(Math.random()*words.length)], 'text/html').documentElement.textContent;
document.body.textContent = p;
( ps: change Math.floor(Math.random()*words.length)
by the one being called on click in the clbk()
function. )
Upvotes: 0