How to do this task in jquery?

Hi there

This is my

What I want before adding that block to a attached blocked I want to change Text of UserText id
before it is added to a block

How can I do this?



I don’t understand the question. Can you rephrase?

Did you mean something like this:

<meta charset="utf-8">
<title>Untitled Document</title>

<div class="appendBlock"></div>
<div id="block" class="block">
		<label class="userText">Your Text Here</label>
		<input type="text" name="userName[]">
		<input type="text" name="userEmail[]">
		<input type="text" name="userAddress[]">
<script src=""></script> 
var e=$("#block");
for(var i=0; i < 3; i++) {
 $('.appendBlock').find('.userText').last().text('Changed text ' + " " + i);

@James_Hibbard can make it nicer :smile:


this is my updated final code
Here I also set value on Input Fields

Not really. This was my attempt : )

var e = $('.block');
for(var i=0; i < 3; i++) {
  var text = "Changed text" + i;


although I understand code but can you explain why we use first and last method while appending value?

What is the differnece in these two


.first() selects the first input element within the div block, which is what I thought you wanted to do.

Ok @Pullo

here I need some more change on that file
this is my JavaScript file

Now what I want row added on StudentDetails block should align with each other mean behave like that are in same table all spaced between are equal

Ok one more question
suppose I have added a delete button

I want div block to append after table but before delete button How can I do this?

Why not try implementing this yourself and let us know if you get stuck?

see my attempt I have posted my that I have attempt so far

I am thinking something like


somebody help on this topic I have posted my Effort

Give it a try using insertAfter() & insertBefore() jquery API’s.

Not sure if i understood this long thread properly but here is my try to make things work your way.

check this fiddle -

jQuery API Links -

Can you do that in case of my code

I know how to use insertAfter and insertBefore

but how in my code?

please check the fiddle link given in last reply.

For your reference -

but why only one row

I have written code for 3 rows at least

You are right buddy @vngx . It should print 3 rows atleast :smile: Let me give it a try one more time :wink:

Check this fiddle -

Thanks Lokesh

I want that Each block should add a Unique Id to it
like 1,2,3 according to value of loop counter

but its not working

That is tricky. To achieve this, we may choose one alternate route by generating HTML on the fly.

Let me know, if it works as per your need.

1 Like

Couldn’t you use the jQuery version of setAttribute? eg.

.attr("id", i )

1 Like