-1

I wish to change the name attribute at dynamic in JavaScript, but I couldn't change the name attribute using either element.name or the setAttribute() method. Is there any other way to set the name attribute?

var status_Element = document.getElementsByName("BD_task_status_"+old_id+"[]");
var final_status_Element = document.getElementsByName("BD_final_task_status_"+old_id+"[]");                         
for(var in_count=0;in_count<status_Element.length;in_count++)
{                   
    status_Element[in_count].name="BD_task_status_level"+inner_count+"[]";
    final_status_Element[in_count].name="BD_final_task_status_level"+inner_count+"[]";
}
7
  • 4
    When you were asking your question, there was a big orange How to Format box to the right of the text area with useful information in it. There was also an entire toolbar of formatting aids. And a [?] button giving formatting help. And a preview area located between the text area and the Post Your Question button (so that you'd have to scan past it to find the button) showing what your post would look like when posted. Making your post clear, and demonstrating that you took the time to do so, improves your chances of getting good answers. Commented Apr 27, 2016 at 12:19
  • Can you please make your code readable? Commented Apr 27, 2016 at 12:19
  • Possible duplicate of Can I use JavaScript to set the 'name' attribute? Commented Apr 27, 2016 at 12:20
  • I've fixed the formatting for you and moved your question to the top. It's almost always best to start with a short version of the question before dumping out code. Commented Apr 27, 2016 at 12:21
  • 1
    I see inner_count being used but never defined - perhaps you meant in_count? Commented Apr 27, 2016 at 12:22

1 Answer 1

4

Setting the name property as in your example will work (other than the inner_count vs in_count typo). The issue is that getElementsByName returns a live collection of elements. When you change the name of the element at index 0, it no longer belongs in the collection and the collection drops it; then the element that used to be at index 1 is now at index 0. But in the meantime, you've incremented in_count and so you never end up changing that element. Half of them will end up not being changed.

Either:

  1. Loop backward through the collection to change the names (since then the element that disappears is at the end and you don't care), or

  2. Use querySelectorAll instead, which gives you a snapshot collection (not a live one). (querySelectorAll is supported on all modern browsers, and also IE8.)

Sign up to request clarification or add additional context in comments.

2 Comments

@ T.J.Crowder. if changed element no longer present in collection means changed element must have the new name . but here no changes could happen
@sakthikumar: That element would have a changed name, but then you'd skip the next. If you're not getting any changes, odds are there's an error in your web console telling you what's wrong (such as the inner_count vs. in_count thing).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.