• 0

[Javascript] Delete default text on form field entry


Question

I've done a little searching and found some javascript that does sort of what I'm after.

  Quote

var addEvent = function(elem, type, fn) { // Simple utility for cross-browser event handling

if (elem.addEventListener) elem.addEventListener(type, fn, false);

else if (elem.attachEvent) elem.attachEvent('on' + type, fn);

},

textField = document.getElementById('myField'),

placeholder = 'Type some text'; // The placeholder text

addEvent(textField, 'focus', function() {

if (this.value === placeholder) this.value = '';

});

addEvent(textField, 'blur', function() {

if (this.value === '') this.value = placeholder;

});

Thing is as I'm going to be creating quite a few forms with various different field names etc, I wanted to know the best way to implement this so that it would clear any field on first click but not the second (so it doesn't delete what the user has entered), rather than having to list out all of the placeholder text and field names.

I hope this makes sense. I may just be over complicating things!

Many thanks :),

Alex

10 answers to this question

Recommended Posts

  • 0
  On 17/01/2013 at 16:28, Axel said:

I've done a little searching and found some javascript that does sort of what I'm after.

Thing is as I'm going to be creating quite a few forms with various different field names etc, I wanted to know the best way to implement this so that it would clear any field on first click but not the second (so it doesn't delete what the user has entered), rather than having to list out all of the placeholder text and field names.

I hope this makes sense. I may just be over complicating things!

Many thanks :),

Alex

Get the value of any form fields after the page loads and store them. Once the field comes into focus, compare the current value with the stored value... if it's the original value, erase it, if not, leave it alone.

Or, placeholder, and everyone without HTML5 can go screw. :laugh:

  • Like 2
  • 0
  On 17/01/2013 at 16:36, threetonesun said:

Get the value of any form fields after the page loads and store them. Once the field comes into focus, compare the current value with the stored value... if it's the original value, erase it, if not, leave it alone.

Or, placeholder, and everyone without HTML5 can go screw. :laugh:

You're assuming i know javascript. Javascript scares me *hides* :laugh:

I was going to use placeholder but I was just after something cross browser. Meh - I'll worry about it later!

Cheers :)

  • 0
  On 17/01/2013 at 16:48, Axel said:

You're assuming i know javascript. Javascript scares me *hides* :laugh:

I was going to use placeholder but I was just after something cross browser. Meh - I'll worry about it later!

Cheers :)

Here's a fiddle for you, using jQuery: http://jsfiddle.net/yrBXc/

  • 0

hmmm cant seem to get it to work.

Inserted it like this:

<script type="text/javascript">

$inputs = $('input, textarea');

$inputs.each(function(){

$val = $(this).val();

$(this).data('initial', $val);
});

$inputs.focus(function(){
 $val = $(this).val() == $(this).data('initial') ?  '' : $(this).val() ;
$(this).val($val);

});

</script>

Where in the document do I need to insert it?

  • 0

You should wrap your code in the document ready method defined here: http://api.jquery.com/ready/.

Basically, using the shorthand method:


<script>
$(function() {
// your code here.
});
</script>
[/CODE]

  • 0

I'm such an idiot! :laugh:

This is likely going to be integrated into a page where more javascript will exist. Is there a way to ensure it doesn't conflict? I seem to remember a noconflict tag or something along those lines from something done in the past. I'll google it!

Cheers fellas! :)

  • 0
  On 18/01/2013 at 19:19, Axel said:

I'm such an idiot! :laugh:

This is likely going to be integrated into a page where more javascript will exist. Is there a way to ensure it doesn't conflict? I seem to remember a noconflict tag or something along those lines from something done in the past. I'll google it!

Cheers fellas! :)

Yup, there's a bunch of ways to do it: http://docs.jquery.com/Using_jQuery_with_Other_Libraries

Mostly regarding the use of $ if that's defined by another library. If it's just js + jQuery, you shouldn't have any issues.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Elden Ring Nightreign will finally gain ‘Duo Expeditions' next week by Pulasthi Ariyasinghe FromSoftware's latest project, Elden Ring Nightreign, delivered a multiplayer-focused experience for the first time. While the title offered solo runs as an alternative to its standard three-player Trio Expeditions, Duos were surprisingly missing at launch. A couple of months later, the developer is finally delivering this highly requested feature for the hit roguelike. On social media, the studio confirmed that Duo Expeditions will be hitting Elden Ring Nightreign with the next update, patch 1.02, for the game on July 30. Check out the newly released trailer showing off this mode below. As we mentioned in our review of Elden Ring Nightreign, everything from enemy health, boss aggression, player damage, and even XP gain changes depending on how many players are in the Expedition. A post-launch update even tweaked the difficulty to favor solos as well. Duos should have the same changes being applied, with the mode being easier than trios but harder than solos to complete. Making sure the difficulty is properly balanced has previously been given as the reason why the Duos mode has taken a while to come in. FromSoftware also confirmed that patch 1.02 will have a range of more quality-of-life improvements on the UI side. This will include more Relics filtering options, making custom builds easier to construct before going on new runs, as seen below. Publisher Bandai Namco also announced this week that the Elden Ring Shadow of the Erdtree expansion for the original open-world Soulslike recently passed 10 million players. Meanwhile, this Elden Ring Nightreign spin-off now has over 5 million players, making both massive successes for the publisher. While FromSoftware is now focusing on other projects, Bandai Namco isn't done with the Elden Ring universe just yet either, as a live-action movie has now been confirmed to be in development too.
    • Or you ignore the part where @Michael Scrip answered you directly.
    • The reasons for Microsoft being in near perpetual state of reorganization for two years is due to the 6 CapEx acquisitions between Jan 2022 and Jan 2023, Blizzard, Oribi, Minit, Miburo, Lumenisity and Fungible. There were 15 CapEx acquisitions in 2021 alone. Much like the Borg, Microsoft assimilates knowledge instead of expending resources on learning and development. 10 gaming companies, 3 AI and 1 data modeling companies since 2018. But then this is in the founding DNA of Microsoft, MS DOS wasn't made by MS, Windows was based on the Apple Mac and Xerox PARC Alto....and on and on. (To be fair Apple's Lisa and Mac were from the Alto also)
    • Can't get a pic of the physical server as it's in a DC, but what i'm running on it is doable. All for my own use except one VM used by a friend to run her site and other things.
    • Immediately using a VPN that makes me appear to be in the EU.
  • Recent Achievements

    • Very Popular
      d4l3d earned a badge
      Very Popular
    • Dedicated
      Stephen Leibowitz earned a badge
      Dedicated
    • Dedicated
      Snake Doc earned a badge
      Dedicated
    • One Month Later
      Philsl earned a badge
      One Month Later
    • One Month Later
      armandointerior640 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      629
    2. 2
      ATLien_0
      238
    3. 3
      Xenon
      163
    4. 4
      neufuse
      123
    5. 5
      +FloatingFatMan
      123
  • Tell a friend

    Love Neowin? Tell a friend!