2

This should straightforward but I have no idea what is wrong. I am making an ajax call when there is onblur event on a text field. Ajax request goes to backend and gets the response. Then I have to append that response next to the text field. But it's not working.
HTML

<input type="text" required="" id="123456" onblur="validateWithDB(this)"></input>

Javascript

function validateWithDB(field)
            {
                $.ajax({
                    type:"POST",
                    url:"validate",
                    data: "fieldValue="+field.value,
                    success: function(response) {
                        alert(response);// this is correct
                        alert($(field).val());// this is correct
                        var result = '<span class="help-inline">'+response+'</span>'
                        alert(result) // this is correct
                        $(field).html(result);// Does not show up.
                    },
                    error:function (xhRequest, ErrorText, thrownError) {
                        alert('Error: '  + '  ' + xhRequest);
                    }
                });
            }

I tried both $(field).html(result); and $(field).append(result); but no luck.

6
  • 3
    You can't put a span into an input... Commented Jul 23, 2013 at 13:02
  • What is the value of 'field'? Commented Jul 23, 2013 at 13:02
  • But he can put text into an input :D Commented Jul 23, 2013 at 13:03
  • I want to put this span next to input field. Commented Jul 23, 2013 at 13:03
  • @JeevanJose field is the input, see his html Commented Jul 23, 2013 at 13:03

3 Answers 3

4

If you want the span NEXT to the input, use .after()

$(field).after(result);
Sign up to request clarification or add additional context in comments.

Comments

3

Try this:

$(field).val(result);

If you want to append span after input use this:

$(field).after(result)

2 Comments

That'll obliterate the user entered value with HTML, not show the error message alongside it
@Rup +1 for "obliterate". HTML is so much more fun with war-metaphors.
2

As tymeJV asid in the comment, you can't put HTML inside the input tag.

You can either set it as the value using $(field).val(result) but this doesn't make much sense.

Or you can insert it after using .after:

$(field).after(result);

This will display the HTML in result directly after the input.

2 Comments

.insertAfter will insert field after result.
Input field disappeared when I used insertAfter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.