0

enter image description here

enter image description here

how document.getelementbyclassname("but") I'm not able to modify the style inside the function. when i try to run the code i get this error enter image description here

  • 1
    [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) Instead, add a [mcve] **as text**. – Ivar Mar 14 '22 at 20:55
  • Instead of changing CSS via JS, use CSS classes, e.g. `.parentCls .but { border: 1px solid #dddfe2; }`; then [`theParent.classList.toggle("parentCls")`](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList). Inline event handlers like `onclick` are [bad practice](https://stackoverflow.com/q/11737873). They’re an [obsolete, cumbersome, and unintuitive](https://stackoverflow.com/a/43459991) way to listen for events. Always [use `addEventListener`](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_—_dont_use_these) instead. – Sebastian Simon Mar 14 '22 at 21:00

2 Answers2

-1

When you use getElementsByClassName you are getting an array of elements. To fix your error, make sure to select the element you need, e.g.:

inputstxt[0].style.border = ..

Or if you want to apply the new styles to all:

for(let i = 0; i < inputstxt.length; i++) {
   inputstxt[i].style.border = ..
}
ale917k
  • 1,494
  • 7
  • 18
  • 37
-1

cannot set properties of undefined means that you are trying to read the style of an Array instead of an element. So you would need to document.getElementsByClassName("but")[i].style //i is any index to get/set the style.

Timm Nicolaizik
  • 357
  • 1
  • 11
  • 1
    FYI, the downvote was probably not for the correctness of your answer, but the fact that you answered a poor quality question. Doing so encourages more such questions. – isherwood Mar 14 '22 at 21:10
  • @isherwood Or for the fact that there are _far_ better options than targeting one specific element to change its CSS properties. – Sebastian Simon Mar 14 '22 at 21:11
  • _“Why would you downvote the answer then?”_ — As the comment said: _“Doing so encourages more such questions.”_. _“Why not the question?”_ — Who says the question wasn’t downvoted? – Sebastian Simon Mar 14 '22 at 21:12