0

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.

s_m
  • 83
  • 1
  • 1
  • 9
  • can you be more specific please? What is the format of the word list? Can you provide us an example of what is printed by `console(words)`? First assumption is that you're looking for [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) property, but can't be sure because of the formatting of your question – Kaiido Mar 05 '15 at 11:34
  • @Kalido ["​​​white out", "​​​wireless mouse", "​​​white board", "​​​water", "​​​wireless keyboard", "​​​wireless router", "​​​wireless printers", "​​​window envelopes"] – s_m Mar 05 '15 at 11:48
  • above type op i am getting – s_m Mar 05 '15 at 11:48

2 Answers2

1

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/

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

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. )

Kaiido
  • 123,334
  • 13
  • 219
  • 285