2

I have this html inputs.

<input type='text' class='req-in' name='fname'>
<input type='text' class='req-in' name='lname'>

I am trying to do to add somr element next to the input field. I tried append() and prepend() but it goes wrong.

this is my code:

$('.req-in').blur(function() {
        $(this).append('<p>some text here for this field only!</p>');   
    });

is this possible? my ideal output would be like this

<input type='text' class='req-in' name='fname'><p>some text here for this field only!</p>
<input type='text' class='req-in' name='lname'>

2 Answers 2

4

You need to use .after() method.

Insert content, specified by the parameter, after each element in the set of matched elements.

$('.req-in').blur(function() {
    $(this).after('<p>some text here for this field only!</p>');   
});

or .insertAfter()

Insert every element in the set of matched elements after the target.

$('.req-in').blur(function() {
 $('<p>some text here for this field only!</p>').insertAfter($(this));
});
Sign up to request clarification or add additional context in comments.

1 Comment

Drummer beating drummer :)
0

Try this one

    <!DOCTYPE html>
<html>
<title>Stack HTML</title>
<link rel="stylesheet" href="../repo/css/bootstrap.css" type="text/css" />
<script src="https://code.jquery.com/jquery-2.1.3.js"></script>
<script src="../repo/js/bootstrap.js"></script>
<script src="../repo/js/jquery.validate.js"></script>
<head></head>
<body>

    <input type='text' class='req-in' name='fname'>
    <input type='text' class='req-in' name='lname'>

    <script>
        $(document.body).on('blur', '.req-in' ,function(){
            $(this).after('<p>some text here for this field only!</p>');
        });
    </script>
</body>
</html>

Comments

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.