1

I'm trying to add some HTML after an input field using jQuery but it's not working and I don't understand why.

<input type="text" value="secret password" maxlength="255" id="ripo" title="Password" class="ms-long ms-spellcheck-true">
$(document).ready(function() {
  jQuery("input[title='Password']").append('<P>test</P>');
});
5
  • 2
    append to an input ????? doesn't make any sense Commented Feb 9, 2017 at 9:57
  • jQuery("input[title='Password']").val('<P>test</P>'); Commented Feb 9, 2017 at 9:58
  • can you append p in input val ^? Commented Feb 9, 2017 at 9:58
  • Sorry I expressed myself somewhat sloppy. I want to add some HTML after an INPUT field, question adjusted. Commented Feb 9, 2017 at 9:59
  • I'd strongly suggest you at least skim read the methods that jQuery has: api.jquery.com. Most of them are self explanatory and give you a good idea of what can be done with the library Commented Feb 9, 2017 at 10:00

3 Answers 3

3

You have to use .after() method.

append method insert content to the end of each element in the set of matched elements.

after method insert content after each element in the set of matched elements.

$(document).ready(function() {
  jQuery("input[title='Password']").after('<p>test</p>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="secret password" maxlength="255" id="ripo" title="Password" class="ms-long ms-spellcheck-true">

Also, you can use insertAfter method in the following way.

$(document).ready(function() {
      $('<p>test</p>').insertAfter($("input[title='Password']"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="secret password" maxlength="255" id="ripo" title="Password" class="ms-long ms-spellcheck-true">

Sign up to request clarification or add additional context in comments.

1 Comment

The 2nd method will append content to the end of .parent() not necessarily after the input. It'll do what OP wanted if the input is the last element in its parent. So .after() is the way to go
3

append() is used to add content within an element, however form inputs do not contain any content. Normally you'd use val() to set their values.

However to achieve what you require use the after() method instead to insert content in to the DOM after the selected element:

$(function() {
  $("#ripo").after('<P>test</P>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="secret password" maxlength="255" id="ripo" title="Password" class="ms-long ms-spellcheck-true">

Comments

0

You cannot append HTML to an input. But you can do this (if you wanted to actually set the value):

$(document).ready(function() {
  jQuery("input[title='Password']").val('test');
});

Although this does not answer the edited question, other answers address appending an element after the input.

2 Comments

Just FYI OP updated the question to be more clear about what he's trying to do. val() isn't the solution here.
have updated my answer and left it in case someone else needs this solution to the question.

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.