Jump to content

Question

Posted

http://jsfiddle.net/QSh64/

The code is above.

I have made it so that onclick the background of the textbox will change from transparent to white. However I can't seem to figure out how to place the label behind the checkbox so it gets hidden. I've tried changing the z-index and nothing happens.

I'm trying to make a placeholder that disappears on click. By default chrome doesnt seem to remove the placeholder until the user starts typing.

Any clues?

Cheers,

Alex

Share this post


Link to post
Share on other sites

13 answers to this question

  • 0

Posted

Can't you solve it with Javascript ?

Something like this :

[CODE]<head>
<script type="text/javascript">
function ClearPlaceHolder (input) {
if (input.value == input.defaultValue) {
input.value = "";
}
}
function SetPlaceHolder (input) {
if (input.value == "") {
input.value = input.defaultValue;
}
}
</script>
</head>
<body>
<input type="text" value="Please fill this field" onfocus="ClearPlaceHolder (this)" onblur="SetPlaceHolder (this)" />
</body>[/CODE]
1 person likes this

Share this post


Link to post
Share on other sites
  • 0

Posted

i'd rather do it pure css if possible. It's part of a big complicated wordpress site so the less i have to put in the head the better. I don't see why this can't be achieved with the method I'm attempting.

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Axel' timestamp='1360590140' post='595514916']
i'd rather do it pure css if possible. It's part of a big complicated wordpress site so the less i have to put in the head the better. I don't see why this can't be achieved with the method I'm attempting.
[/quote]

You missed position: relative from the input, so right now the z-index is useless and the label is ontop of the input so when the background colour is changed it doesn't hide it.

Share this post


Link to post
Share on other sites
  • 0

Posted

You can use the [font=courier new,courier,monospace]+[font=arial,helvetica,sans-serif] combinator to select a sibling of the focused input element:[/font][/font]
[CODE]
.the-form input:focus + label.placeholder {
display: none;
}
[/CODE]
This will hide the adjacent placeholder whenever the input gains focus. [font=courier new,courier,monospace][font=arial,helvetica,sans-serif][url="http://jsfiddle.net/MattiasBuelens/pCMM6/"]I forked your fiddle[/url] and also set the [font=courier new,courier,monospace]position[/font] on the input to [font=courier new,courier,monospace]relative[/font] so[/font][/font] the different [font=courier new,courier,monospace]z-index[/font] actually takes effect, as suggested by Jamie.

Note however that when the user types in the input and the input loses focus again, the placeholder re-appears. There's no CSS pseudo-class for distinguishing (non-)empty inputs, so you'll need a bit of JavaScript either way to hide the placeholder when the input is filled in.

Share this post


Link to post
Share on other sites
  • 0

Posted

[url="http://jsfiddle.net/UHKJt/"]http://jsfiddle.net/UHKJt/[/url]

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Axel' timestamp='1360590140' post='595514916']
i'd rather do it pure css if possible. It's part of a big complicated wordpress site so the less i have to put in the head the better. I don't see why this can't be achieved with the method I'm attempting.
[/quote]

You're using other javascript scripts on the page, right? Compile them into one file and minimize it, it's a much better solution than non-semantic label hiding in css.

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Axel' timestamp='1360589363' post='595514894']
By default chrome doesnt seem to remove the placeholder until the user starts typing.
[/quote]
Thats because it's a much better implementation of placeholders, lets say the user is looking at keyboard, they press tab, place holder is hidden.. it's best for placeholder to be removed when the user starts typing..

unless your doing it for something specific i'm struggling to see why you would go to the effort to do this when it provides a worse user experience.

Share this post


Link to post
Share on other sites
  • 0

Posted

Only because the person I'm doing the coding for wants it done unfortunately. I'll take a look at these methods and report back - thanks!

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Axel' timestamp='1360597073' post='595515120']
Only because the person I'm doing the coding for wants it done unfortunately. I'll take a look at these methods and report back - thanks!
[/quote]

ok fair enough, maybe try to educate them why it's a bad ux decision :)

Share this post


Link to post
Share on other sites
  • 0

Posted

I just thought of a REALLY simple solution!:-

<input id="code-field" type="text" placeholder="Postal Code" onfocus="placeholder=''" onblur="placeholder='Postal Code'">

onfocus and onblur events to change the placeholder text :)

http://jsfiddle.net/UQdFR/
1 person likes this

Share this post


Link to post
Share on other sites
  • 0

Posted

[code]input:focus::-webkit-input-placeholder
{
color: transparent;
}
[/code]

Share this post


Link to post
Share on other sites
  • 0

Posted

Well that is another workaround, but you have to remember that many older Web Browsers (also modern Internet Explorer) don't support the "placeholder" attribute !

[url="http://www.w3schools.com/tags/att_input_placeholder.asp"]http://www.w3schools...placeholder.asp[/url]

You could also do this in a sort-of hybrid thing. Use Placeholder when support and Javascript when necessary.

[CODE]<script>
// placeholder polyfill
$(document).ready(function(){
function add() {
if($(this).val() == ''){
$(this).val($(this).attr('placeholder')).addClass('placeholder');
}
}

function remove() {
if($(this).val() == $(this).attr('placeholder')){
$(this).val('').removeClass('placeholder');
}
}

// Create a dummy element for feature detection
if (!('placeholder' in $('<input>')[0])) {

// Select the elements that have a placeholder attribute
$('input[placeholder], textarea[placeholder]').blur(add).focus(remove).each(add);

// Remove the placeholder text before the form is submitted
$('form').submit(function(){
$(this).find('input[placeholder], textarea[placeholder]').each(remove);
});
}
});
</script>[/CODE]

Share this post


Link to post
Share on other sites
  • 0

Posted

don't reinvent the wheel.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.