Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using javascript with css?

Tags:

javascript

css

when i did click text input, i want to change input background color but with javascript.

I know with css.

I want for example.

<form action="" name="abc" method="post">
<input name="abc" id="name" type="text"/>
<input name="yusuf" type="text"/>
<input type="submit"/>
</form>

How can i select element name as name with javascript?

I want similar:

 <script>
document.getElementById("name").style = "background-color:red";
</script>

I want to write css code but direct. Shortly how can i reach style="" parameters?

like image 231
Yusuf ali Avatar asked Feb 23 '23 03:02

Yusuf ali


2 Answers

You're looking for the style object:

document.getElementById("name").style.backgroundColor = "red";

You can also set the className property to apply an existing CSS class to the element.

like image 105
SLaks Avatar answered Feb 25 '23 18:02

SLaks


Is jQuery an option? You can try something like this:

$(document).ready(function(){
    var toggleRed = function(){
        $(this).toggleClass('red');
    };
    $('input[type=text]').focus(toggleRed);
    $('input[type=text]').blur(toggleRed);
});

And then define this in your CSS:

input.red { background-color: Red; color: White; }

Check out the working example: http://jsfiddle.net/V5qJp/

like image 42
Milimetric Avatar answered Feb 25 '23 18:02

Milimetric