2

First of all the question will be simple to you but i have a doubt that whether can we use name value instead of id or class like this

var name_type=$('.abc_type').attr('name','type');

i just tried like this but of no use

var name_type=$('input[name=type]').attr('name','type');
11
  • As I understand, you want to add a "name=type" to the ".abc_type" classed element? Feb 20, 2018 at 7:35
  • "but of no use": of course it isn't you're setting the name attribute to value "type" on input elements which have a name attribute with value "type".
    – Richard
    Feb 20, 2018 at 7:35
  • @DiabloSteve yes that i mean
    – user_777
    Feb 20, 2018 at 7:37
  • Does your code can select the '.abc_type'? I tried this, and my js can modified name to type. Feb 20, 2018 at 7:38
  • @DiabloSteve yes my code can select .abc_type
    – user_777
    Feb 20, 2018 at 7:39

1 Answer 1

3

You can unse any of this attribute selectors in Jquery: https://api.jquery.com/category/selectors/ Find which you want to use and then, is the same as get it by .class or #id

Consider the lenght of the element, because return every element wich has the selector condition.

Use your selector: var name_type=$('input[name=type]').attr('name','type');

Then iterate over that:

$.each(name_type, function(index, value){
  console.log("Index: "index + ", Value: "+value);
  if(name_type[index].value == "I want this"){
    //do something or terurn whatever
  }
})

Hope helps

3
  • if multiple input fields have same name then how can we fetch the correct value,actually i was stuck in there so i chooses this way but unfortunately using class and id always getting same value
    – user_777
    Feb 20, 2018 at 7:53
  • can you edit your question and add that casuistics? About your first question, if you iterate over the elements, you can check all the info them... so wich has the the correct value 4u? wich one who the user selected? why you dont identify your inputs with unique id?
    – Oscar
    Feb 20, 2018 at 8:22
  • stackoverflow.com/q/48866731/5064637 here is my question
    – user_777
    Feb 20, 2018 at 9:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.