Javascipt to hide by class and the name [closed]

I want to hide by the class and the name attribute in div

This wont work

$("div.ind_post").find("[name^='1']").hide();

This work

$("div.ind_post").hide();
$("div").find("[name^='1']").hide();

I wonder why i can hide by the element with class, or the name, but i cannot hide with class and name both. so my question is how to hide within the class, by a specify name, thanks!


Solution 1:

find searches descendants

Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

You should do

$("div.ind_post[name^='1']").hide();

I suspect the reason why $("div").find("[name^='1']").hide(); worked is that you had some div element higher up in the DOM hierarchy

Solution 2:

Keeping in mind that name attributes are not allowed on div elements.

find searches the descendants of the selected elements.

If you want an element which matches a class selector and an attribute selector then you need to either search for them together in the first place:

 $("div.ind_post[name^='1']")

or filter the collection of matched elements on the extra rule

 $("div.ind_post").filter("[name^='1']").hide();