1
<input name="Indian_Karnataka_Bangalore" value="Bangalore" />
<input name="Indian_Andhra_Hyderabad" value="Hyderabad" />
<input name="Indian_Kerala_Trivandrum" value="Trivandrum" />
<input name="Indian_Maharashtra_Mumbai" value="Mumbai" />

At a given time, only one input element will be present in the DOM. How will I know the name of the specific input element name? I don't want to depend on values as it might change. Using jQuery.

The INDIAN term will be static in every input element.

Actually i am trying to validate the input elements. DOM will have all the elements but at a given time only one element will be active and that element should have some value in it.

mridula
  • 3,203
  • 3
  • 32
  • 55
Syed
  • 25
  • 3
  • 1
    If only one will ever be present, simply go for the first and get it's name. – Yoshi Apr 29 '13 at 08:26
  • what do you mean by `elements of similar name`? can you give an example – Arun P Johny Apr 29 '13 at 08:28
  • 2
    possible duplicate of [JQuery: Select multiple select tags of a name](http://stackoverflow.com/questions/3663647/jquery-select-multiple-select-tags-of-a-name) – Felix Kling Apr 29 '13 at 08:31
  • I think you need to be clear about what you want? Do you want to get the `name` attribute value? Also, if only one of them will ever be present, then what defines which one to begin with? Surely you can use that to work out the name and then you can use that to work our the exact selector you need – musefan Apr 29 '13 at 08:33
  • Actually i am trying to validate the input elements. DOM will have all the elements but at a given time only one element will be active and that element should have some value in it. the given answers just doesn't help me. – Syed Apr 29 '13 at 09:34
  • What do you mean by 'active'? Does it mean only one element will be visible and others will be hidden? – mridula Apr 29 '13 at 09:45

5 Answers5

2
var $inputs = $('input[name*="Indian"]'),
    inputsName = $inputs.attr('name');

You can use the same selectors as you would CSS.

Chris Coyier wrote a piece on attribute selectors here

ahren
  • 16,803
  • 5
  • 50
  • 70
  • Your answer was better before, I don't think he want's the actual 'name' value... that would be pointless – musefan Apr 29 '13 at 08:31
  • @musefan - Reverted. He did say he wants the name attribute value, but I think he was meaning *how will I select it without knowing the name*. – ahren Apr 29 '13 at 08:32
  • 1
    Yeah that is what I was thinking too. Perhaps you can add both for a more complete answer – musefan Apr 29 '13 at 08:34
  • Actually i am trying to validate the input elements. DOM will have all the elements but at a given time only one element will be active and that element should have some value in it. the given answers just doesn't help me. – Syed Apr 29 '13 at 09:35
  • @Syed - maybe you need to rephrase your question then. – ahren Apr 29 '13 at 09:36
1
var indianInputs = $("input[name^='Indian']"); 
//Matches all input elements whose name attrributes 'begin' with 'Indian'

This differs than the one posted by @ahren in that his selector will match all input elements whose name attribute contain the string 'Indian'.

indianInputs.attr("name");

Would return the first matched element's name attribute's value, which, for your markup will be Indian_Karnataka_Bangalore

To find the names of all indianInputs, you must iterate over all matched elements

var indianInputNames = [];
indianInputs.each(function() {
    indianInputNames.push($(this).attr("name"));
});
c.P.u1
  • 16,664
  • 6
  • 46
  • 41
  • Just a note - you're breaking your selector string. Check the type of quotes you're using. – ahren Apr 29 '13 at 08:39
  • 1
    Sorry bro, had the proper string first, edited it to the wrong one when formatting. Fixed it now. Thanks :) – c.P.u1 Apr 29 '13 at 08:40
0
 $('input[name="element_name"]')

You have a lot of ways to select by the name of the attribute check http://api.jquery.com/category/selectors/attribute-selectors/

Antonio Papa
  • 1,596
  • 3
  • 20
  • 38
0

Try

var name = $('input[name^="Indian_"]').attr('name')
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Have you tried the filter function? Something like this:

$('input:visible')
    .filter(function() {
        return $(this).attr("name").match(/^Indian/);
});

This will return an array of input elements whose name starts with "Indian". There is a good example here: https://stackoverflow.com/a/193787/1237117.

Community
  • 1
  • 1
mridula
  • 3,203
  • 3
  • 32
  • 55