Javascript onfocus event handler will only allow me to carry out one function
I'm a bit of a rookie with Javascript so I apologise now if the answer is something obvious. My problem is that, on focusing on a given textarea, I want to both clear its contents and change the display value of a separate div from 'none' to 'block'. Doing these things separately wasn't a problem.
Clear contents:
onfocus="if (this.value == 'Update your status') {this.value = '';}"
and changing the display property of the separate div:
onfocus="displayControls();"
<script type="text/javascript">
function displayControls() {
if (document.getElementById('statusUpdaterControls').style.display == "none") {
document.getElementById('statusUpdaterControls').style.display = "block";
};
};
</script>
When I have then tried to combine the two, either in the onfocus event of the textarea, or in the displayControls() function above like this:
<script type="text/javascript">
function displayControls() {
if (document.getElementById('statusUpdater').value == 'Update your status') {
document.getElementById('statusUpdater').value = '';
};
if (document.getElementById('statusUpdaterControls').style.display == "none") {
document.getElementById('statusUpdaterControls').style.display = "block";
};
};
</script>
suddenly neither solution works... Any help would be greatly appreciated.
EDIT: I have found a solution - by putti开发者_开发百科ng the clear content code into an onclick event and keeping them separate, both items are carried out. I would still appreciate any insight into what I was doing wrong earlier however as I must have been doing something silly I can improve on for the future. Thanks in advance.
The onfocus
accepts a function, not a string, so:
something.onfocus = displayControls;
精彩评论