How do we execute a javaScript function on focus of the element?

 Posted by Vivek.Ramapuram on 8/2/2014 | Category: CSS 3 Interview questions | Views: 1897 | Points: 40
Answer:

Here in this we have a textbox with onfocus event.When we focus(means when we place the cursor)on the textbox ,GetFocus executes that recieves the id of the textbox In this function, we are finding the texbox (using its id passed in to function) and setting its background color to “blue”.

example:

Enter your name: <input type="text" name="txtName" id="txtName" onfocus="GotFocus(this.id)" />

<input type="button" id="btn" name="btn" value="Submit" />
<script type="text/javascript">
function GotFocus(id) {
document.getElementById(id).style.background = "#c0c0c0";
}
</script>


Asked In: While Learning | Alert Moderator 

Comments or Responses

Login to post response