Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  134] [ 6]  / answers: 1 / hits: 190743  / 15 Years ago, wed, april 22, 2009, 12:00:00

this is a text field, new_id is an integer.



When I apply the following snippet:



$(this).attr('id',   this.id + '_' + new_id);
$(this).attr('name', this.name + '_' + new_id);
$(this).attr('value', 'test');


the id changes, the name changes too, but not the value. If I change the last line to this (and therefore use a string literal)



$('#mytextfield_3').attr('value', 'test');


it works.



Any ideas?



-- EDIT --
Thanks to Steerpike for the quick plugin test - i believe it should work, but i can't find the error.



Here's some more code:



I create the clone using



clone = $(this).find('.clone_fields_container:first').clone();


clone now contains a div which has embedded input fields.



After generating a new id:



  /** Iterate over input and select fields in container */

clone.children('input,select,textarea').each(function() {
$(this).attr('id', this.id + '_' + new_id);
$(this).attr('name', this.name + '_' + new_id);
$(this).val('test');
console.log(this);
});


The text fields do not have any values.


More From » jquery

 Answers
104

I just wrote a quick plugin to run a test using your same snippet and it works fine



$.fn.test = function() {
return this.each(function(){
var new_id = 5;
$(this).attr('id', this.id + '_' + new_id);
$(this).attr('name', this.name + '_' + new_id);
$(this).attr('value', 'test');
});
};

$(document).ready(function() {
$('#field_id').test()
});

<body>
<div id=container>

<input type=text name=field_name id=field_id value=meh />
</div>
</body>


So I can only presume something else is going on in your code. Can you provide some more details?


[#99666] Thursday, April 16, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianom

Total Points: 601
Total Questions: 98
Total Answers: 109

Location: Kenya
Member since Fri, Dec 23, 2022
1 Year ago
lucianom questions
Tue, Feb 22, 22, 00:00, 2 Years ago
Wed, May 5, 21, 00:00, 3 Years ago
Sun, Jan 24, 21, 00:00, 3 Years ago
Sat, Aug 15, 20, 00:00, 4 Years ago
Mon, Jun 22, 20, 00:00, 4 Years ago
;