Categories
HTML JavaScript jQuery

How to Find an Element by Name with jQuery

We can find an element with the given name with jQuery by using the $ itself.

For instance, if we want to find something with the name foo, we can write:

$('[name=foo]')

The general form of this CSS selector is:

[attribute=value]

In the example above name is the attribute and foo is the value of the name attribute.

Then that will return the element with the name foo.

Once we did that, if the element exists on the page, then we can do anything with it like adding/removing classes, adding/removing styles, and so on.

Categories
HTML JavaScript jQuery

How to Find an Element by ID with jQuery

We can find an element with the given ID with jQuery by using the $ itself.

For instance, if we want to find something with ID foo, we can write:

$('#foo')

Then that will return the element with the ID foo.

Once we did that, if the element exists on the page, then we can do anything with it like adding/removing classes, adding/removing styles and so on.