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');
Vasyl Moskalov
  • 4,242
  • 3
  • 20
  • 28
user_777
  • 845
  • 1
  • 9
  • 25

1 Answers1

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

Oscar
  • 1,929
  • 3
  • 16
  • 31
  • 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 '18 at 07: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 '18 at 08:22
  • https://stackoverflow.com/q/48866731/5064637 here is my question – user_777 Feb 20 '18 at 09:21