Ivo San
Ivo San

Reputation: 1253

Adding attr within class with name=name[]

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

Answers (3)

user1864610
user1864610

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

Aldi Unanto
Aldi Unanto

Reputation: 3682

How about this?

function addID()
{   
   $('.container input[name="name[]"]').attr('id', 'inputID');
}

Upvotes: 1

zerkms
zerkms

Reputation: 255005

$('.container input[name="amount[]"]').attr('id', 'foobar');

http://api.jquery.com/attribute-equals-selector/

Upvotes: 2

Related Questions