Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Textbox's color doesbt change

Hello!I have this code:

HTML:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>IP Address Info</title>


</head>
<body>

<script>



</script>

<label style="background-color:whitesmoke;color: black;position:fixed;top:20px;left:20px;width:100px;height:20px; font-family:Verdana">IP Address



</label>
<label style="background-color:whitesmoke;color: black;position:fixed;top:20px;left:340px;width:200px;height:20px; font-family:Verdana">Subnet mask



</label>

<input type="text" id="ipAddressId" style="background-color:whitesmoke;color:gray;position:fixed;border-color:whitesmoke;top:120px;left:20px;font-family:Verdana;width:200px;height:20px; border-radius:8px" onfocus="function f(){this.HTMLElement().setAttribute('color','black')}">








</body>
</html>

I would like the text of my textbox to change color when it is on focus(or selected) but it doesnt change and I dont know where I am wrong.Any help?
 
Hey there, the CSS :focus pseudo-class is what you should be using, but it will have to be in an internal or external stylesheet for it to work, not inline.

Since you will have to use an internal or external stylesheet anyway, you may as well just write all of you CSS internally or externally for the sake of readability.

Here's a re-written version of your code, but with an internal stylesheet and the :focus pseudo-class:
HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>IP Address Info</title>
    <style>
      #ipAddressLabel {
        background-color: whitesmoke;
        color: black;
        position: fixed;
        top: 20px;
        left: 20px;
        width: 100px;
        height: 20px;
        font-family: Verdana;
      }

      #subnetMaskLabel {
        background-color: whitesmoke;
        color: black;
        position: fixed;
        top: 20px;
        left: 340px;
        width: 200px;
        height: 20px;
        font-family: Verdana;
      }

      #ipAddressId {
        background-color: whitesmoke;
        color: gray;
        position: fixed;
        border-color: whitesmoke;
        top: 120px;
        left: 20px;
        font-family: Verdana;
        width: 200px;
        height: 20px;
        border-radius: 8px;
      }

      /* Use :focus pseudo-class for focus state */
      #ipAddressId:focus {
        color: black;
      }
    </style>
  </head>
  <body>
    <label id="ipAddressLabel">IP Address</label>
    <label id="subnetMaskLabel">Subnet mask</label>
    <input type="text" id="ipAddressId">
  </body>
</html>

Now, if you were to use JavaScript for the focus state, this is what you should be doing:
HTML:
<input onfocus="this.style.color = 'black'"> <!-- other attributes have been removed for this example -->
Keep in mind that the colour will stay the same when the element loses focus, so you will have to add an onblur listener if you want to change the colour back to default.
 
Last edited:
I don't know if I'm being rude for making another person's code more compact unprompted...
HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>IP Address Info</title>
    <style>
      #ipAddressLabel,
      #subnetMaskLabel,
      #ipAddressId {
        background-color: whitesmoke;
        color: black;
        position: fixed;
        top: 20px;
        left: 20px;
        width: 100px;
        height: 20px;
        font-family: Verdana;
      }

      #subnetMaskLabel {
        left: 340px;
        width: 200px;
      }

      #ipAddressId {
        color: gray;
        border-color: whitesmoke;
        top: 120px;
        width: 200px;
        border-radius: 8px;
      }

      #ipAddressId:focus {
        color: black;
      }
    </style>
  </head>
  <body>
    <label id="ipAddressLabel">IP Address</label>
    <label id="subnetMaskLabel">Subnet mask</label>
    <input type="text" id="ipAddressId">
  </body>
</html>
Also, onfocus="this.style.color = 'black';" makes the field use the text black even after unfocusing.
In my opinion, the only code worth is onfocus="this.select();", although I'm afraid of using this and only use event.target.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom