elize elize - 4 months ago 8
HTML Question

set some text after input with name input via jQuery

hi there I have a code like this:

<input name="name1" type="text"/>
<div class="error"></div>

<input name="name2" type="text"/>
<div class="error"></div>

<input name="name3" type="text"/>
<div class="error"></div>


I want set some text in div elements via JQuery.
for example fill
<div class="error"></div>
after 'name3'

Answer

In your case use next().

$(function(){
  $('input[name="name3"]').next('.error').text("Hello");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="name1" type="text"/>
<div class="error"></div>

<input name="name2" type="text"/>
<div class="error"></div>

<input name="name3" type="text"/>
<div class="error"></div>

Comments