Reputation: 1253
In the example below, I can add an attribute 'id' to a class='container' But I don't know how to add an attribute 'id' to one of the inputs. Let's say I wanted to add an attribute 'id' to an input with name='name[]'
how can I do this?
<div class='container'>
<input name='amount[]' />
<input name='other' />
</div>
function addID()
{
$('.container').attr('id', 'containerID');
}
Upvotes: 0
Views: 75
Reputation:
This will probably work:
function addID()
{
$('.container input[name="name[]"').attr('id', 'containerID');
}
But be aware that if it returns more than one matching input they'll all get the same id
. id
should be unique on the page.
Upvotes: 1
Reputation: 3682
How about this?
function addID()
{
$('.container input[name="name[]"]').attr('id', 'inputID');
}
Upvotes: 1
Reputation: 255005
$('.container input[name="amount[]"]').attr('id', 'foobar');
http://api.jquery.com/attribute-equals-selector/
Upvotes: 2