- This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
Viewing 7 posts - 1 through 7 (of 7 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › need javascript help [1]
<input placeholder="Password.." onfocus="y()" type="text"/>
function y(){
this.style.backgroundColor="red";
}
whys this not working, can someone help?
@Alen – I’m not exactly sure what you’re solving with that pen…
Regardless, in your example you don’t need Javascript at all to accomplish what you’re trying to do. Styling is what CSS is for:
input[type="text"]:focus {
background: red;
}
Also there’s a proper input type for passwords:
<input type="password" placeholder="Password" />
http://codepen.io/ggilmore/pen/51dd201291632b5d45c631c53d13bb89
He edited the question to add input. But yeah at least he knows the pointless JavaScript approach. :)
getElementsByClassName
returns an array of elements.
Since our two boxes are different, we have to select them individually. If we had many box1
then you would have to use for loop to style them. Since we know we only have one item in the array, using [0]
selects the element we want.
I guess you could do this: http://codepen.io/anon/pen/DifpE but the box[n]
part becomes too ambiguous.
I do just didnt log in when I forked the pen.
And js is not my thing, am still learning.
I know enough to know that I don’t know. Lol if that makes sense.