Alvaro Gomez
Alvaro Gomez

Reputation: 360

Search children by name

How could it be done to search for a child's div by its name comparing it by the name with a given variable's string value?

This is the idea of what i'm aming:

var vname="whatever";
if($("#container").children().attr('name')==vname{
    $("#container").children().attr(vname).addClass("selected");
}

Upvotes: 0

Views: 70

Answers (3)

Shikiryu
Shikiryu

Reputation: 10219

You can use some filters too

var vname="whatever";
$("#container")
    .children()
    .filter(function(){
        return $(this).attr('name') == vname;
    })
    .addClass("selected");

Upvotes: 0

Satpal
Satpal

Reputation: 133403

You can try

 $("#container").children("[name="+vname+"]").addClass('selected');

Upvotes: 1

Jason P
Jason P

Reputation: 27012

You can use the attribute equals selector:

$('#container [name="' + vname + '"]').addClass('selected');

Or if you only want direct children:

$('#container > [name="' + vname + '"]').addClass('selected');

Upvotes: 6

Related Questions