2

If I have 5 input boxes with the class .inputBox my jquery function adds these tags 5 times after each of the .inputBox lines. Why is that?

I just want each of these tags inserted once after each .inputBox.

Anyone know how to do that?

function addImages() {          

     $(".inputBox").each(function() {

          $('.inputBox').after("<img src=\"Images/thumbs_up_48.png\" class=\"thumbV\" id=\"up\" />");
          $('.inputBox').after("<img src=\"Images/thumbs_down_48.png\" class=\"thumbV\" id=\"down\" />");
    });
}

the html

<label for="FirstName">First Name</label>
<input type="text" class="inputBox" name="FirstName" title="First Name Here" id="firstName" />

3 Answers 3

1

Use:

$(this).after("<img src=\"Images/thumbs_up_48.png\" class=\"thumbV\" id=\"up\" />");
$(this).after("<img src=\"Images/thumbs_down_48.png\" class=\"thumbV\" id=\"down\" />");

$('.inputBox') will iterate through the entire DOM every time you call it.

An even better way would be

$(".inputBox")
        .after("<img src=\"Images/thumbs_up_48.png\" class=\"thumbV\" id=\"up\" />")
        .after("<img src=\"Images/thumbs_down_48.png\" class=\"thumbV\" id=\"down\" />");

And don't forget to 'accept' an answer that works for you.

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

1 Comment

None of the answers worked in my other posting. I found the .each function and tried this out and your answer worked. Thanks!
0

You can simply lose the each and just use

 $('.inputBox').after("<img src=\"Images/thumbs_up_48.png\" class=\"thumbV\" id=\"up\" />");
 $('.inputBox').after("<img src=\"Images/thumbs_down_48.png\" class=\"thumbV\" id=\"down\" />");

1 Comment

Sorry guys. I left a piece out for easier readability. I need the .each because I use $('inputBox').each(function(index) and i use the index value appended to the id up and down so it'd be up+index and down+index.
0

You probably don't need the each, do you? Based on your code and fix proposed by patrick, i think you can just use your original code without the each() call.

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.