6

Sorry if this is too simple, I am new to programming and I stumbled upon this problem. How can I use the name attribute as the selector inside a
$(document).on('click', '#thisisforid', function().. ?

It is well documented that I can use the id and class name by using #idname and .classname but it is nowhere to be found (I tried so hard to search) how to or if I could use the name attribute instead.

super-user
  • 1,017
  • 4
  • 17
  • 41
  • 2
    `'[name="your_name"]'` – Tolgahan Albayrak Feb 06 '17 at 14:19
  • http://api.jquery.com/?s=attribute –  Feb 06 '17 at 14:28
  • 1
    @squint the jQuery website search engine is crap. – ibrahim mahrir Feb 06 '17 at 14:29
  • @ibrahimmahrir: It's not great, yet that simple query brings one to a list of selectors that do what the OP is asking. Making it slightly more specific brings the list even closer. http://api.jquery.com/?s=+attribute+selector –  Feb 06 '17 at 14:30
  • @super-user: Also note that the jQuery API is broken down into categories on the left. So you can go right to [selectors](http://api.jquery.com/category/selectors/) for a full list, or to [attribute selectors](http://api.jquery.com/category/selectors/attribute-selectors/) for a very specific list. –  Feb 06 '17 at 14:35
  • 1
    You can also simply use Google to [search stackoverflow.com](https://www.google.com/search?q=site%3Astackoverflow.com+jquery+selector+by+name+attribute) –  Feb 06 '17 at 14:37
  • 1
    "*I tried so hard to search*" - can't have tried that hard, a simple google search of your question "How can I use the name attribute as the selector": https://www.google.co.uk/search?q=How+can+I+use+the+name+attribute+as+the+selector+jquery – Pete Feb 06 '17 at 14:47

3 Answers3

4

Try this: $(document).on('click', '*[name="3"]', function().. the * should not be necessary in most cases.

4

Use an Atrribute Selector like this:

$("[name = 'theNameYouWant']");

More CSS Selectors here.

Note: The CSS Selectors are not exclusive features of jQuery. They work everywhere (in CSS files, using document.querySelector ...)

ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
1

You can do it this way.

$( "[name*='man']" ).on('click', function(){
alert("worked!!");
})

You can refer this docs for attribute selection in jQuery

https://api.jquery.com/attribute-contains-selector/

Ravi Teja Kumar Isetty
  • 1,559
  • 4
  • 21
  • 39