tags:

views:

46

answers:

3

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" />
+1  A: 

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.

patrick dw
None of the answers worked in my other posting. I found the .each function and tried this out and your answer worked. Thanks!
Catfish
A: 

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\" />");
Vinodh Ramasubramanian
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.
Catfish
A: 

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.

psychotik