0

I want to give a

-webkit-box-shadow:inset 0 0 5px black;

property to a text box as one focuses on it.

For example, here the background-color is being changed, but I want box-shadow instead.

<script>
function myFunction(x)
{
x.style.background="yellow";
}
</script>

and the HTML

<input type="text" onfocus="myFunction(this)">
2
  • Try jQuery perhaps? Especially if you're doing a lot of this kind of thing Commented Mar 13, 2013 at 18:33
  • I'm confused. Are you asking about an input or a div? Commented Mar 13, 2013 at 18:37

3 Answers 3

3

Why not use :focus in CSS?

http://jsfiddle.net/VjLKV/

input {
    border: 1px solid black;
}

input:focus {
    outline: none;
    border: 1px solid black;
    -webkit-box-shadow:inset 0 0 5px black;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thankx. I didn't knew something like that exsisted. Sincerely, I am new to web designing and am 12 years old.
Wisdom: When I first started, I was under the general understanding that javascript could do anything, if it couldn't be done a different way, javascript was the solution. While this in some ways can be very true, I learned to first explore every other possible way, especially when it comes to stylistic things. If CSS can't do it, javascript/jquery probably can, but if you can do it in CSS, it's almost always a better way of doing it.
0

Define css classes, and you can easily switch them as needed.

<script>
function myFunction(x)
{
x.style.className="yourcssclass";
}
</script>

Comments

0

Instead of using JS for this, use the CSS pseudo-class :focus:

input[type="text"]:focus
{
    -webkit-box-shadow:inset 0 0 5px black;
    box-shadow:inset 0 0 5px black;
}

That way, the textbox will get the shadow when the user tabs into it, or clicks it, or whatever - and the styles will properly go away when they leave, and so on.

Remember to use the unprefixed version of the box-shadow style as well.

1 Comment

You have :focus confused for :active.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.