5 Replies - 11938 Views - Last Post: 30 October 2012 - 06:20 PM

#1 LiDoNg_9_0   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 159
  • Joined: 03-September 09

Changing input name using JQUERY

Posted 19 August 2011 - 12:46 AM

Im trying to change all the input name based on the list index of <li>
But it seems like it's not changing at all.
$('.test').click(function() {
for(var i=0;i<$("li.songs").length;i++)
{
  var ob = $("li.songs").eq(i).clone();
  ob.find('input').each(function()
  {
    this.name = this.name+i;
    alert(this.name); //just checking if does it change
  });		
}
});



now, the alert displays the right name i want, BUT no changes on the name when I inspect the element AND try to submit the form and display all the POST values. Any help ??

example expected output:
[Before changing]
<li class="songs"><input type="text" name="song" /></li>
<li class="songs"><input type="text" name="song" /></li>
<li class="songs"><input type="text" name="song" /></li>

[After Changing]
<li class="songs"><input type="text" name="song1" /></li>
<li class="songs"><input type="text" name="song2" /></li>
<li class="songs"><input type="text" name="song3" /></li>


NOTE:!!
I DONT WANT THE input named song to be an ARRAY. just telling you from the start.. XD

Is This A Good Question/Topic? 0
  • +

Replies To: Changing input name using JQUERY

#2 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: Changing input name using JQUERY

Posted 19 August 2011 - 03:05 AM

Why don't you just do:
$('input[name="song"]').each(function(index) {
    this.name = this.name + (index + 1);
});


Untested, but should work fine.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Changing input name using JQUERY

Posted 19 August 2011 - 07:14 AM

We have a jQuery subforum, so in the future post jQuery-related questions there.

Moved.
Was This Post Helpful? 0
  • +
  • -

#4 LiDoNg_9_0   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 159
  • Joined: 03-September 09

Re: Changing input name using JQUERY

Posted 19 August 2011 - 05:36 PM

View PostJackOfAllTrades, on 19 August 2011 - 07:14 AM, said:

We have a jQuery subforum, so in the future post jQuery-related questions there.

Moved.


Sorry, just got confused with the category. Still, jquery is a javascript right ?? :P
Was This Post Helpful? 0
  • +
  • -

#5 Kluther   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-October 12

Re: Changing input name using JQUERY

Posted 26 October 2012 - 02:07 AM

works for me! But what if you just added a row with Jquery?

View PostRudiVisser, on 19 August 2011 - 03:05 AM, said:

Why don't you just do:
$('input[name="song"]').each(function(index) {
    this.name = this.name + (index + 1);
});


Untested, but should work fine.

Was This Post Helpful? 0
  • +
  • -

#6 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Changing input name using JQUERY

Posted 30 October 2012 - 06:20 PM

View PostLiDoNg_9_0, on 19 August 2011 - 07:36 PM, said:

Sorry, just got confused with the category. Still, jquery is a javascript right ?? :P


JQuery is a Javascript library. Some people may not be using it and it only serves to clutter the forum if we don't keep threads where they, theoretically, should be.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1