In Jquery, How to Select an Element by Its Name Attribute

How do I select an element with its name attribute in jQuery?

$('[name="ElementNameHere"]').doStuff();

jQuery supports CSS3 style selectors, plus some more.

See more

  • jQuery - Selectors
  • jQuery - [attribute=""] selector

How can I select an element by name with jQuery?

You can use the jQuery attribute selector:

$('td[name="tcol1"]')   // Matches exactly 'tcol1'
$('td[name^="tcol"]' ) // Matches those that begin with 'tcol'
$('td[name$="tcol"]' ) // Matches those that end with 'tcol'
$('td[name*="tcol"]' ) // Matches those that contain 'tcol'

jQuery, how to find an element by attribute NAME?

Simply use deleteuserid instead of deleteuserid!="" like following.

console.log($('[deleteuserid]'));

In jQuery, how do I select an element by its name attribute?

This should do it, all of this is in the documentation, which has a very similar example to this:

$("input[type='radio'][name='theme']").click(function() {
var value = $(this).val();
});

I should also note you have multiple identical IDs in that snippet. This is invalid HTML. Use classes to group set of elements, not IDs, as they should be unique.

jQuery - How to select value by attribute name starts with

If you want all data-* attributes, you can iterate through jq data object:

$('.slide').each(function(){
for(data in $(this).data())
console.log(data); // returns confirmID so element as an attribute `data-confirmID`
});

But this data object can contains other keys which aren't attribute, setted for example by some plugins.

EDIT

To get all kinds of attribute to "starts with", you can customize your own jQuery selector:

jQuery.extend(jQuery.expr[':'], {    attrStartsWith: function (el, _, b) {        for (var i = 0, atts = el.attributes, n = atts.length; i < n; i++) {            if(atts[i].nodeName.toLowerCase().indexOf(b[3].toLowerCase()) === 0) {                return true;             }        }                return false;    }});
//e.g:$('.slide:attrStartsWith("data-")').css('color', 'red');$('.slide:attrStartsWith("conf")').css('color', 'blue');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><div class="slide" data-confirmID="46" data-testID="666">1</div><div class="slide" confirmID="54" >2</div>

jquery how select element by inverse name attribute

You can use the :not() selector to make the modification, or just make a css rule for it and do without the javascript all together.

Though, if you choose the pure css way, you'll most likely want to scope the selector down so it is not so global.

$('a:not([safe])').addClass('not-safe')
.not-safe {  color: red;  border: 2px solid red;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><ul>  <a href="#">Link Base</a>  <a href="#">Link Base</a>  <a href="#">Link Base</a>  <a href="#">Link Base</a>  <a safe href="#">Link Base</a>  <a safe href="#">Link Base</a></ul>

Get select value by name attribute using jQuery

to get the value of your selected option you don't need such a complicated selector in jquery, plus .text() is not the right function to get the value:

$('select[name=ccountries]').val();

is enough.



update:

I don't know anything about ASP, but in your context this would be:

var country = $(this).parent().parent().parent().find('select[name=ccountries]').val();

How to refer to a drop down list by name using jquery

Say you have a select element with id my-drop-down like this :

<select name="myName" id='my-drop-down'></select>

You can refer it like this:

var referenceToDropDown = $('#my-drop-down');

OR like this:

 var referenceToDropDown = $('select[name = "myName"]');


Related Topics



Leave a reply



Submit