jQuery: $(document).on(event, name as selector, function [duplicate] - javascript

This question already has answers here:
How can I select an element by name with jQuery?
(14 answers)
Closed 6 years ago.
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.

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

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

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/

Related

How to change a html class with Javascript? [duplicate]

This question already has answers here:
How can I change an element's class with JavaScript?
(33 answers)
Closed 7 years ago.
I would like to Change a HTML class with JavaScript
HTML
<i id="test" class="fa">
How can I do it really simple?
Try this:
use .className to add and also get class name of particular element.
document.getElementById('test').className = "fa";
Aside from the className property, you can also use the much more elaborate classList property and its methods:
var ele = document.getElementById('test');
ele.classList.remove('fa');
ele.classList.add('ba');
You can also toggle() classes (with optional conditions!) and hence easily do without jQuery’s toggleClass() and its other class-related methods (like shown in the downvoted, accepted answer).
Try to use this in javascript:
$("#test").removeClass("fa");
$("#test").addClass("newClass");

which is better way from performance point of view either use "Id" as selector or "Class" as selector in Jquery [duplicate]

This question already has answers here:
JQuery class selector vs id selector
(5 answers)
Closed 8 years ago.
HTML:
JQuery:
('.nameClass').click(myFunction);
or
('#txtName').click(myFunction);
In Jquery Which is best perform when we have 100's of control to apply this logic. And Why better perform ?
Using id selector will be fast always as it returns single element but class selector will return one or more elements.
NOTE - You must ensure that id should be unique through out the DOM.

Find all elements with an 'id' attribute on the DOM [duplicate]

This question already has answers here:
jquery get only all html elements with ids
(3 answers)
Closed 8 years ago.
Simple. I want to traverse the DOM and find all elements that have an id attribute. Can someone help me with a js and/or jquery script
You can query all the elements with an id attribute by using the following jQuery selector:
$("[id]")
You can also just use plain JavaScript using querySelectorAll:
document.querySelectorAll("[id]")
If you want to find non-empty id attributes, use this selector instead:
'[id]:not([id]="")'
Simple attribute selector
$('[id]');
Reference : Has Attribute Selector [name]

Select element by class or ID [duplicate]

This question already has answers here:
jQuery OR Selector?
(6 answers)
Closed 9 years ago.
I am working on an ASP.NET page in which I generate some JavaScript code based on values in a database.
For example, in the database there might be the formula sum([#itemA]), which would evaluate to something like $('#itemA').change(function(){ sum(this); });.
I know that I can select elements using jQuery selectors such as $('[id*=itemA]') or $('[class*=itemA]'), but I am wondering if there is any way to combine these selectors so that any element with either a class or an ID of itemA would be selected.
Right now in my code I am just using if/else blocks to deal with ID's or classes, but if there is an easier implementation, I would love to use it.
I looked at the jQuery documentation and googled around a bit, but I didn't see anything that answered my question.
Thanks in advance!
This should do the trick
$('[id*=itemA], [class*=itemA]')
For more details, take a look to official jquery documentation
Use multiple selectors like
$('.class1, .class2....., .classn')
You can literally use anything like ID names, class names or selectors like you specified separated by commas
Like in css, when selectors are separated by ,they are cumulated :
var elements = $('[id*=itemA], [class*=itemA]')

Get Index Question - JQuery [duplicate]

This question already has answers here:
Select element by index (multiple elements of same class)
(4 answers)
Closed 2 years ago.
Possible Duplicate:
Select element by index (multiple elements of same class)
Quick question, I'm targeting all the article elements in my html5 page using:
var articles = $("article");
What I want to do is target one of the article elements using an index only. I can't seem to get it to work, any ideas?:
articles[1].css("display", "none"); // <-- This won't work
The array is returning the DOM element rather than the jQuery object. The .css() function does not exist on the DOM element, so you can wrap it in with the jQuery $ function to create a jQuery object that you can call .css() on.
Try $(articles[1]).css("display", "none");
Demo
Edit: Or even better articles.eq(1).hide();
You can use the .eq() function to target a specific index,
$("article").eq(1).css("display", "none");
According to the jQuery documentation referenced above,
Reduce the set of matched elements to
the one at the specified index.
Try this. This should target the first article
var articles = $('article').eq(0);
articles.css({"display":"none"});
Check this out for more of an explanation but this does exactly what you need.
http://api.jquery.com/eq/

Categories