0

I have a grid with values

I need to disable the textbox with ID as minutes for 2nd row. Following sample does not work.

document.getElementById("h_ID")[2].readOnly = true;

Can someone help me with the syntax?

user2093576
  • 3,082
  • 7
  • 32
  • 43

2 Answers2

1

Only one element can have a given id in a HTML document.

That's why document.getElementById("minutes") doesn't return a collection but a unique element. Don't use id in your case but classes.

So your code will be

 document.getElementsByClassName("minutes")[2].readOnly = true;
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • As explained in my answer, your HTML is invalid. Use classes instead of id. – Denys Séguret Nov 12 '15 at 09:30
  • I have to use IE7, getElementsByClassName wont work there – user2093576 Nov 12 '15 at 09:43
  • If you really need to use IE7, you should use a helper library to query elements, for example jQuery or sizzle. You'll find alternate solutions [here](http://stackoverflow.com/questions/3808808/how-to-get-element-by-class-in-javascript) if you don't want to. – Denys Séguret Nov 12 '15 at 09:51
1

document.getElementById returns only one element, because you are not supposed to have more than one element with an id on the page. Switch all the id="minutes" to class="minutes" and then use:

document.getElementsByClassName('minutes')[2].readOnly = true;
metal03326
  • 1,213
  • 2
  • 13
  • 15
  • am using struts2, if i add class instead of id it shows yellow warning sign – user2093576 Nov 12 '15 at 09:38
  • You should add the class to the cssClass attribute like that: – metal03326 Nov 12 '15 at 09:40
  • I have to use Ie7.. getElementsByClassName wont work here – user2093576 Nov 12 '15 at 09:42
  • You can add that functionality by the following code: document.getElementsByClassName = document.getElementsByClassName || function(cl) { var retnode = []; var elem = this.getElementsByTagName('*'); for (var i = 0; i < elem.length; i++) { if((' ' + elem[i].className + ' ').indexOf(' ' + cl + ' ') > -1) retnode.push(elem[i]); } return retnode; }; – metal03326 Nov 12 '15 at 09:47