Solved

JavaScript function required for form onfocus and onblur

Posted on 2016-11-21
2
62 Views
Last Modified: 2016-11-22
I have a form as below, onfocus and  onblur where previously working but not anymore.

Can anyone provide a JavaScript function to do the same thing and should I try loading at the bottom of the page.

Thanks in advance


<form action="members-search" method="GET">
<br /> 
<div id="get_my_location" class="ui-widget"><label for="suburb">Where do you need a Service Professional?</label><br /> 

<input type="text" value="Enter Post Code or Suburb" name="suburb" id="suburb" 

onfocus="if(this.value == 'Enter Post Code or Suburb'){ this.value = ''; }"

onblur="if(this.value == ''){ this.value = 'Enter Post Code or Suburb'; }"> 

<br />
<input id="set_suburb" type="submit" value="Get Started" /></div><br /> 
</form>

Open in new window

0
Comment
Question by:sabecs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 41896803
just use the HTML5 placeholder attribute : https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-placeholder
test page : https://jsfiddle.net/3716ym9j/2/

else with javascript :
https://jsfiddle.net/3716ym9j/1/

<form action="members-search" method="GET">
  <br /> 
  <div id="get_my_location" class="ui-widget"><label for="suburb">Where do you need a Service Professional?</label><br /> 
  <input type="text" value="Enter Post Code or Suburb" name="suburb" id="suburb" /> 
  <br />
  <input id="set_suburb" type="submit" value="Get Started" /></div><br /> 
</form>
<script>
	var on = function(event, id, code) {
  	if(document.getElementById(id).addEventListener)
    	document.getElementById(id).addEventListener(event, code, false);
    else
    	document.getElementById(id).attachEvent("on"+event, code);
  }
	on("focus", "suburb", function() {
  	if(this.value == 'Enter Post Code or Suburb')
    	this.value = '';
  });
	on("blur", "suburb", function() {
  	if(this.value == '')
    	this.value = 'Enter Post Code or Suburb';
  });
</script>

Open in new window

1
 

Author Closing Comment

by:sabecs
ID: 41898328
Thanks, very much appreciated, I never new about the placeholder HTML5 option, that is perfect.
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquery Ajax - on change event not being picked up 9 64
javascript duplicates 9 26
Google map API and Coldfsuion 4 27
Javascript function 3 21
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question