0

I have this jQuery line:

jQuery("input[name=secondname10]").removeAttr("disabled").val("");

I'd like to know how can I make the name dynamic instead of entering it in the code as I did with name=secondname10.

RightSaidFred
  • 10,594
  • 31
  • 34
user1073001
  • 193
  • 1
  • 1
  • 7

3 Answers3

4
$('input[name=' + myNameVariable + ']')

From your comment, it's not quite clear what you want.

  • If you want to apply this to all inputs, you could just write $('input').
  • If you want to apply it to all inputs with an name, you could write $('input[name]');.
  • If you want to apply it to all inputs with a name that starts with secondname you could write $('input[name^=secondname]').

Chances are the inputs you want to target have some more convenient things in common, by which you can access them. Perhaps you're looking for all inputs in a certain container? Or all inputs with a certain class?

I suggest you read up on jQuery selectors for an idea of the different means by which you can identify a set of objects for manipulation.

David Hedlund
  • 121,697
  • 28
  • 196
  • 213
  • Thanks. Is there a way to get the name of the input dynamically? because i have other names secondname10, secondaname14, secondname14, email15, adresse15, etc. and i if i create a new field i must enter the name of the input in the code. I'd like that it get this name dinamically. I tried "this.name" but it doesn't work – user1073001 Dec 01 '11 at 14:28
1

Create a function that has a name parameter.

function enableInput(name) {
    jQuery("input[name="+ name +"]").removeAttr("disabled").val("");
}

then you can do this

enableInput("secondname10");

if you want to do this on every input element you can do this

jQuery("input").removeAttr("disabled").val("");

if you want to do this on every input type text you can do this

jQuery("input[type='text']").removeAttr("disabled").val("");

and so on...

As you can see you have to apply the right filter to your selector.

hope this helps

dknaack
  • 56,707
  • 23
  • 140
  • 187
  • 2
    `hideInput` is a peculiar name for a function that enables and clears an input o_O – David Hedlund Dec 01 '11 at 14:19
  • Or you could make it more like a DOM function, and call it `enableInputByName`, then add in code to make it behave unpredictably in IE. :p – RightSaidFred Dec 01 '11 at 14:25
  • Thanks. Is there a way to get the name of the input dynamically? because i have other names secondname10, secondaname14, secondname14, email15, adresse15, etc. and i if i create a new field i must enter the name of the input in the code. I'd like that it get this name dinamically. I tried "this.name" but it doesn't work. – user1073001 Dec 01 '11 at 14:25
  • Do you want to do this with every input field on the page ? – dknaack Dec 01 '11 at 14:27
  • @user1073001: Is all this for an element that you just dynamically created? – RightSaidFred Dec 01 '11 at 14:30
  • Yes, thats it. that's why i want to get the name instead of add a jquery line for each name of the input. – user1073001 Dec 01 '11 at 14:30
  • @user1073001: I think you need to describe what you *actually* need in your question. Give a detailed description of the issue. For example, if the issue is that you need to know what the next number should be for your new element, then you need to explain that. Also, provide more code. That will likely answer some questions as well. – RightSaidFred Dec 01 '11 at 14:32
  • @user1073001 maybe you should add more informations to your question. i have updated my answer. hope this helps – dknaack Dec 01 '11 at 14:35
0

you mean like tihs?

function removeAttr(nameVariable) {
     $("input[name=" + nameVariable + "]").removeAttr("disabled").val("");
}
Johnie Karr
  • 2,634
  • 2
  • 32
  • 41
  • Thanks. Is there a way to get the name of the input dynamically? because i have other names secondname10, secondaname14, secondname14, email15, adresse15, etc. and i if i create a new field i must enter the name of the input in the code. I'd like that it get this name dinamically. I tried "this.name" but it doesn't work – user1073001 Dec 01 '11 at 14:28