javascript - Add HTML after an input element with jQuery - Stack Overflow

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

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>');
});

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>');
});
Share Improve this question edited Feb 9, 2017 at 10:59 Mihai Alexandru-Ionut 48.5k14 gold badges105 silver badges132 bronze badges asked Feb 9, 2017 at 9:56 daddedadde 6591 gold badge12 silver badges27 bronze badges 5
  • 2 append to an input ????? doesn't make any sense – Pranav C Balan Commented Feb 9, 2017 at 9:57
  • jQuery("input[title='Password']").val('<P>test</P>'); – Pranav C Balan Commented Feb 9, 2017 at 9:58
  • can you append p in input val ^? – guradio 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. – dadde Commented Feb 9, 2017 at 9:59
  • I'd strongly suggest you at least skim read the methods that jQuery has: api.jquery.. Most of them are self explanatory and give you a good idea of what can be done with the library – Rory McCrossan Commented Feb 9, 2017 at 10:00
Add a ment  | 

3 Answers 3

Reset to default 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./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./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">

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./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">

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745179427a4615351.html

相关推荐

  • javascript - Add HTML after an input element with jQuery - Stack Overflow

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

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信