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.

7
  • 2
    '[name="your_name"]' Commented Feb 6, 2017 at 14:19
  • api.jquery.com/?s=attribute Commented Feb 6, 2017 at 14:28
  • 1
    @squint the jQuery website search engine is crap. Commented Feb 6, 2017 at 14:29
  • 1
    You can also simply use Google to search stackoverflow.com Commented Feb 6, 2017 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": google.co.uk/… Commented Feb 6, 2017 at 14:47

3 Answers 3

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 ...)

Sign up to request clarification or add additional context in comments.

Comments

4

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

2 Comments

nicely done! thanks!
No problem, glad I could help.
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/

2 Comments

thanks for the help! +1
thats not the same as $(document).on('click', "*[name='man']", function() { });

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.