0

When a user clicks on the textbox, the font in the textbox should change color, but for some reason it does not.

I have the following code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(function() {
      $(".textbox0").click(function() {
        $(this).css('color', 'rgb(64, 0, 128)');
      });
    });
  </script>
  <style type="text/css">
    .textbox0 {
      position: fixed;
      left: 131px;
      top: 38px;
      font-family: Arial;
      font-size: 8px;
      font-weight: normal;
    }
  </style>
</head>

<body>
  <div class="textbox0"><textarea rows=7 cols=30>Change colour</textarea></div>
</body>
</html>

I believe I did everything correctly as this method worked with buttons, but I guess I didn't.

4 Answers 4

2

This is NOT because your click binding needs to be on the textarea.

Nor is it because you need to use a named CSS color.

Nor is it related to the onDomReady event.

All you need to make your code work is a simple line of CSS:

textarea { color: inherit; }

The reason the text in your textarea isn't changing color is that the user-styles for most browsers are of higher cascade precedence than inherit. This is good, otherwise you would see some very bizarre looking textareas floating around. ;)

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

Comments

1

You need to apply the change to the textarea element, not the div:

$(".textbox0 textarea").click(function() {
    $(this).css('color', 'rgb(64, 0, 128)');
});

Here's a working example.

If you need to keep the click event handler bound to the .textbox0 element and not the textarea, you could use the find (or children) method within the event handler to apply the change to the textarea:

$(".textbox0").click(function() {
    $(this).find("textarea").css("color", "rgb(64, 0, 128)");
});

6 Comments

It'd be great if you would explain why this is, but I have a hunch you aren't so sure yourself.
I do know why this is (and there is little point in my explaining it now since you have done so in your answer). Why do you have a hunch that I would not know that? Also, as the OP would like to change the colour of the textarea, what happens with your method if there is more text in the div at some point? Applying the change to the div would cause an unwanted change to that text, although it would affect the textarea too.
You're making the assumption A) the div serves a purpose other than manipulating the position of the textarea, B) additional text will be added to it in the future, and C) the application of the color to the textarea is unintentional. Teach a man to fish…
And you're making the opposite assumptions! Both of our methods have their advantages, and both have their disadvantages. Both are valid answers.
I'm not making any assumptions. Read the first sentence in the question where the expectation and problem are spelled out plainly. I pointed out the problem. There's not anything inherently wrong with making assumptions to better guide, I just think it makes more sense to actually guide than just share the "corrected" version and sending the asker along none the wiser about the problem.
|
1

You should add the "textarea" tag in your accessor.

I put the code here : http://jsfiddle.net/qfvVv/

Comments

0

The change in the stylesheet is being done on the div, not on the textarea inside the div.

You can change this in several ways: Put the class on the textarea or change your jQuery selector to .textbox0 textarea or change the code in the click event to change the css of the textarea in the div.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.