search

Home  >  Q&A  >  body text

Change text font style using button click in React

<p><pre class="brush:php;toolbar:false;">const textBold =()=>{ const bold_text=document.getElementsByClassName('.form-control'); bold_text.style.fontStyle = "italic"; setText(bold_text) }</pre> <p>After clicking the button, the browser will jump to a blank page. </p>
P粉127901279P粉127901279458 days ago573

reply all(1)I'll reply

  • P粉895187266

    P粉8951872662023-08-29 12:03:58

    Since you are using the getElementsByClassName method, it will return an array/collection containing the elements of the given class.

    If you only have one element, use .querySelector instead.

    Alternatively, if you prefer to use the getElementsByClassName method, you can use a for loop or the .find() method to find the desired element and apply the style.

    In React, I think using ref is better than using the DOM API to locate elements.

    reply
    0
  • Cancelreply