Cyberomin
Cyberomin

Reputation: 523

Removing an element from the DOM using jQuery

$("#phoneR").find("input#phone").remove();
for (var i = 1; i <= para[3] - 1; i++) {
    $("#phoneR").find("td:eq(1)").after(" <input type='text' name='phone[]' disabled='disabled'/>");
}

The code above is suppose to iterate and add the input 3 times, but for some reasons what soever it's not doing. Is it that.

Am i doing something wrong.

@cyberomin

Upvotes: 0

Views: 183

Answers (2)

rakis
rakis

Reputation: 78

Have you checked the result of this?

$("#phoneR").find("td:eq(1)")

td:eq(1) retrieves the second td element that is found inside $("#phoneR"). If you really want the first td, then you'll have to do:

 $("#phoneR").find("td:eq(0)")

Try to do $("#phoneR").find("td:eq(1)").length and make sure it equals 1.

Alternatively, you can use the :first selector, which is equivalent to :eq(0)

$("#phoneR").find("td:first")

Upvotes: 2

Zathrus Writer
Zathrus Writer

Reputation: 4331

I have the feeling this would probably sound as a stupid question, but what's wrong with this approach?

for (var i = 1; i < 3; i++)

I mean, if you want to add exactly 3 inputs, then i < 3 would suffice

anyways, since you most probably will require the para variable, I would suggest caching it before going into the loop, as this would need to calculate para[3] - 1 three times instead of one

var para_3 = para[3] - 1;
for (var i = 1; i <= para_3; i++)

Upvotes: 0

Related Questions