admin管理员组文章数量:1022843
Basically two parts of the question.
- How to put explanatory text inside textboxes in a form (like writing e-mail in the text area of a text box in a form) with styles (i.e. greyed out so that meant to suggest) like on facebook homepage.
- How to put cursor focus on a particular form-field as soon as the page loads (like google sign-in or facebook sign-in)
Is there javascript involved in any of the above procedures? if yes, please specify
- a non-javascript workaround and/or
- the javascript code doing what is needed
Basically two parts of the question.
- How to put explanatory text inside textboxes in a form (like writing e-mail in the text area of a text box in a form) with styles (i.e. greyed out so that meant to suggest) like on facebook homepage.
- How to put cursor focus on a particular form-field as soon as the page loads (like google sign-in or facebook sign-in)
Is there javascript involved in any of the above procedures? if yes, please specify
- a non-javascript workaround and/or
- the javascript code doing what is needed
- I'm not sure what's going here, but don't downvote unless a) you're absolutely sure the answer is actually wrong. People need to read blog.stackoverflow./2009/03/… – cgp Commented Jun 23, 2009 at 15:08
- ya one jQuery guy had to remove his post and still earn a badge perhaps. downvoting it wasn't required. ignorance is the best form of downvoting. vote up whatever you like, leave the rest. – OrangeRind Commented Jun 23, 2009 at 15:19
6 Answers
Reset to default 5The first trick is just a preset value. And when you click the field it has an onclick function that checks. Is the value "Write your email here". If so, clear the field. If not do nothing.
It then has an onblur function that checks, is the field empty. If so, print "Write your email here" in it.
The 2nd one is JavaScript as well.
Here is an Example page with both these functions
<html>
<script type="text/javascript">
function searchBox()
{
elem = document.getElementById("search");
if(elem.value == "Search...")
{
elem.value = "";
}
}
function searchBoxBlur()
{
elem = document.getElementById("search");
if(elem.value == "")
{
elem.value = "Search...";
}
}
function init()
{
document.getElementById("other_box").focus();
}
window.onload = init;
</script>
<body>
<input type="text" name="other_box" id="other_box" />
<input type="text" name="search" id="search" value="Search..." onclick="searchBox()" onblur="searchBoxBlur()" />
</body>
</html>
The first, quick and dirty:
<input name="foo" value="Default text OMG!" onfocus="if(this.value == 'Default text OMG!') this.value = ''"/>
Making that onfocus
event a function in an external JavaScript file is remend. That would yield:
External script:
function checkText(el,someText){ if(el.value == someText) el.value = ''}
Your input:
<input name="foo" value="Default text OMG!" onfocus="checkText(this,'Default text OMG!')"/>
The second: Give the input that you want "focused" an ID and do something like this:
<input id="foo" name="foo" value="Default text OMG!"/>
And in a script:
window.onload = function(){ document.getElementById('foo').focus() }
This will require javascript (via the jQuery framework). The following was quickly typed-up, and untested. May require some minor tinkering. If you have questions - ask :)
- Auto-focus Desired Element
- Change Element Text Color when out of focus
- Show default text, when user-provided text isn't present
/* Run our work once the document is ready (loaded */
$(document).ready(function(){
/* Set initial state of field */
$("input.first-name").val("First Name").css("color", "#CCCCCC");
/* Attach logic to the .focus() event of our input field */
$("input.first-name").focus(function(){
/* What to do when this field is focused */
$(this).val("").css("color", "#333333");
}).blur(function(){
/* What to do when the field is blurred */
if ($(this).val() == "") {
/* Set value to 'First Name,' and set color to light gray */
$(this).val("First Name").css("color", "#CCCCCC");
}
});
/* Autofocus our field */
$("input.first-name").focus();
});
There is no non-JavaScript way to do the first part of your question. As far as the second part is concerned, it can be easily done with JavaScript, but even without it, most modern browsers add focus to the first input field.
JavaScript code
Just set the value to the input field to your preset value:
function focus_function() { if (this.value=="Enter email here") { this.value=""; this.class="actualEmail"; } } function blur_function() { if (this.value=="") { this.value="Enter email here"; this.class="preset"; } }
You can have two CSS classes:
preset
andactualEmail
, and specify different colors, etc.Just have an onload function which puts focus on the required input field:
window.onload = function() { document.getElementById("email_field").focus(); }
There are non-standard attributes you may consider: placeholder
(works in safari i guess) and autofocus
(a part of HTML5 spec).
- implement
placeholder
in Javascript - You can do the similar with
autofocus
(just fireInputElement.focus()
)
myField.focus();
That should place the cursor in myField when the page loads (if you place it at the end or in a $(document).ready()
jQuery block). I have seen some inconsistent behavior with this in different browsers, so your mileage may vary.
As far as setting the color of a text field's input, I would set it to have a default value and add a separate CSS class when the user clicks into it:
And your CSS would look like:
.grayedInputField {
color: gray;
}
.activeInputField {
color: black;
}
And when the user clicks into the field, have javascript add a class to the field to change the text color, like so (again, jQuery):
$(".grayedInputField").focus(function(){$(this).addClass("activeInputField")});
Basically two parts of the question.
- How to put explanatory text inside textboxes in a form (like writing e-mail in the text area of a text box in a form) with styles (i.e. greyed out so that meant to suggest) like on facebook homepage.
- How to put cursor focus on a particular form-field as soon as the page loads (like google sign-in or facebook sign-in)
Is there javascript involved in any of the above procedures? if yes, please specify
- a non-javascript workaround and/or
- the javascript code doing what is needed
Basically two parts of the question.
- How to put explanatory text inside textboxes in a form (like writing e-mail in the text area of a text box in a form) with styles (i.e. greyed out so that meant to suggest) like on facebook homepage.
- How to put cursor focus on a particular form-field as soon as the page loads (like google sign-in or facebook sign-in)
Is there javascript involved in any of the above procedures? if yes, please specify
- a non-javascript workaround and/or
- the javascript code doing what is needed
- I'm not sure what's going here, but don't downvote unless a) you're absolutely sure the answer is actually wrong. People need to read blog.stackoverflow./2009/03/… – cgp Commented Jun 23, 2009 at 15:08
- ya one jQuery guy had to remove his post and still earn a badge perhaps. downvoting it wasn't required. ignorance is the best form of downvoting. vote up whatever you like, leave the rest. – OrangeRind Commented Jun 23, 2009 at 15:19
6 Answers
Reset to default 5The first trick is just a preset value. And when you click the field it has an onclick function that checks. Is the value "Write your email here". If so, clear the field. If not do nothing.
It then has an onblur function that checks, is the field empty. If so, print "Write your email here" in it.
The 2nd one is JavaScript as well.
Here is an Example page with both these functions
<html>
<script type="text/javascript">
function searchBox()
{
elem = document.getElementById("search");
if(elem.value == "Search...")
{
elem.value = "";
}
}
function searchBoxBlur()
{
elem = document.getElementById("search");
if(elem.value == "")
{
elem.value = "Search...";
}
}
function init()
{
document.getElementById("other_box").focus();
}
window.onload = init;
</script>
<body>
<input type="text" name="other_box" id="other_box" />
<input type="text" name="search" id="search" value="Search..." onclick="searchBox()" onblur="searchBoxBlur()" />
</body>
</html>
The first, quick and dirty:
<input name="foo" value="Default text OMG!" onfocus="if(this.value == 'Default text OMG!') this.value = ''"/>
Making that onfocus
event a function in an external JavaScript file is remend. That would yield:
External script:
function checkText(el,someText){ if(el.value == someText) el.value = ''}
Your input:
<input name="foo" value="Default text OMG!" onfocus="checkText(this,'Default text OMG!')"/>
The second: Give the input that you want "focused" an ID and do something like this:
<input id="foo" name="foo" value="Default text OMG!"/>
And in a script:
window.onload = function(){ document.getElementById('foo').focus() }
This will require javascript (via the jQuery framework). The following was quickly typed-up, and untested. May require some minor tinkering. If you have questions - ask :)
- Auto-focus Desired Element
- Change Element Text Color when out of focus
- Show default text, when user-provided text isn't present
/* Run our work once the document is ready (loaded */
$(document).ready(function(){
/* Set initial state of field */
$("input.first-name").val("First Name").css("color", "#CCCCCC");
/* Attach logic to the .focus() event of our input field */
$("input.first-name").focus(function(){
/* What to do when this field is focused */
$(this).val("").css("color", "#333333");
}).blur(function(){
/* What to do when the field is blurred */
if ($(this).val() == "") {
/* Set value to 'First Name,' and set color to light gray */
$(this).val("First Name").css("color", "#CCCCCC");
}
});
/* Autofocus our field */
$("input.first-name").focus();
});
There is no non-JavaScript way to do the first part of your question. As far as the second part is concerned, it can be easily done with JavaScript, but even without it, most modern browsers add focus to the first input field.
JavaScript code
Just set the value to the input field to your preset value:
function focus_function() { if (this.value=="Enter email here") { this.value=""; this.class="actualEmail"; } } function blur_function() { if (this.value=="") { this.value="Enter email here"; this.class="preset"; } }
You can have two CSS classes:
preset
andactualEmail
, and specify different colors, etc.Just have an onload function which puts focus on the required input field:
window.onload = function() { document.getElementById("email_field").focus(); }
There are non-standard attributes you may consider: placeholder
(works in safari i guess) and autofocus
(a part of HTML5 spec).
- implement
placeholder
in Javascript - You can do the similar with
autofocus
(just fireInputElement.focus()
)
myField.focus();
That should place the cursor in myField when the page loads (if you place it at the end or in a $(document).ready()
jQuery block). I have seen some inconsistent behavior with this in different browsers, so your mileage may vary.
As far as setting the color of a text field's input, I would set it to have a default value and add a separate CSS class when the user clicks into it:
And your CSS would look like:
.grayedInputField {
color: gray;
}
.activeInputField {
color: black;
}
And when the user clicks into the field, have javascript add a class to the field to change the text color, like so (again, jQuery):
$(".grayedInputField").focus(function(){$(this).addClass("activeInputField")});
本文标签: phpHow to put explanatory text inside textboxes in a form and focus cursor on fieldsStack Overflow
版权声明:本文标题:php - How to put explanatory text inside textboxes in a form and focus cursor on fields? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745566707a2156491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论