0

I want get of name old input (Input 1) and add it with a change to new name input (Input 2).

DEMO: http://jsfiddle.net/F8SLR/

EX : i have this name: typeservice[] and i get it and change to: typeservice_un[]

I try as:

html:

<input type="text" name="typeservice[]" value="Input 1" id="000">
<input type="submit" value="CLICK ME" class="Chng">
    <br>
<input type="text" name="" value="Input 2" id="111">

js:

$(document).on('click','.Chng', function(e){
    e.preventDefault();
    var name = $('#000').attr('name');
    alert(name+'_un[]')
    //$('#111').attr('name',name+'_un[]');
})

What do i do?

9
  • 2
    possible duplicate of Jquery change name attribute Commented Feb 25, 2014 at 14:19
  • 3
    also, IDs must not start with a number. Commented Feb 25, 2014 at 14:20
  • so you think you wont be downvoted just for putting the pic a model? lol a bad question is a bad question dude Commented Feb 25, 2014 at 14:21
  • The code you have appears to work. What issue are you seeing? Commented Feb 25, 2014 at 14:23
  • Well everything seems fine if you remove comment works on your fiddle. Make sure to change id. It must not start with number and that can give error on some browser. Commented Feb 25, 2014 at 14:24

1 Answer 1

0

I've changed the following line in your fiddle:

var name = $('#abc').attr('name').split('[]').join('');

Basically you have to remove the brackets from the end of the line then append your new text. Also, I've also changed your ids to letters instead of numbers as this is best practice.

DEMO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.