Hi,

Just before I jump from the roof :-)

Need some help here - autosuggest jQuery:

<!DOCTYPE html>

        <head>
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
            <script type="text/javascript">
                $(function() {
                    var timer;
                    $("#txt1").on('keyup', function(e) {
                        var self=this;
                        clearTimeout(timer);
                        if (self.value.length===0) {
                            $("txtHint").text('');
                        }else{
                            timer = setTimeout(function() {
                                $.ajax({
                                    type: "GET",
                                    url: "gethint.asp?q="+self.value,
                                }).done(function( hint ) {
                                    $("#txtHint").text(hint);
                                });
                            }, 500);
                        }
                    });
                });
        </script>
        </head>
        <body>
            <h3>Start typing a name in the input field below:</h3>
            <form action=""> 
                First name: <input type="text" id="txt1" />
            </form>
            <p>Suggestions: <span id="txtHint">... some content ...</span></p> 

        </body>
        </html>​​

One small problem: I enter some text, but change my mind and remove everything.
When the text is removed this field appears without content:
<span id="txtHint">... some content ...</span>

Because of this code:
$("txtHint").text('');

But I want it to continue to say "... some content ..." even after I've removed the text in the text box.

Can be done with this method, but then there will be a lot of unnecessary coding:
$("txtHint").text('... some content ...');

Perhaps poorly explained, but it is still someone who can help me?

Best regards. Vetle

Recommended Answers

All 3 Replies

Just change line 12 to:

$("txtHint").text('... some content ...');

<span id="txtHint">... some content ...</span>

Yes, I understood. But I do not want to write content 2 times (see my thread above).

If I write a lot of content inside:
<span id="txtHint"> ... some content ... </ span>

... I must also write the same here:
$ ("txtHint"). text ('... some content ...');

Which is unnecessary coding ....

Okay. When your script starts, retrieve and store the content in a variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.