Hi

I'm having trouble with a simple html form. I'm trying to have the input box read a simple message, such as "enter ticket number here", and allow the user to click anywhere within the input box to clear the message and begin entering their number.

The two problems I'm having, and I have no idea why, is

#1 clearing the message when a user clicks anywhere within the box

#2 allowing the user the ability to click anywhere within the box

I can get it to work outside the page that it's in now, but within the page it doesn't work at all.

I attached the file below

Thanks for any help!

Dani AI

Generated

Quick recap and a few robust options that address the remaining question about clearing a prompt inside an input (after discovered the overlay issue and demonstrated an inline onfocus fix).

Modern, simple approach: use the HTML5 placeholder attribute and keep a proper label for accessibility. The placeholder is shown visually but is not submitted as the field value.

<label for="ticket">Ticket number</label>
<input id="ticket" name="ticket" placeholder="Enter ticket number here" />

JavaScript fallback for older browsers or when a visible default value is required (feature-detect placeholder, clear only if the current value matches the default, restore on blur, and avoid destroying user input):

<script>
(function(){
  var input = document.getElementById('ticket');
  if (!('placeholder' in document.createElement('input'))) {
    var defaultVal = 'Enter ticket number here';
    input.value = defaultVal;
    input.addEventListener('focus', function(){
      if (this.value === defaultVal) this.value = '';
      // optionally: this.select();
    }, false);
    input.addEventListener('blur', function(){
      if (this.value === '') this.value = defaultVal;
    }, false);
  }
})();
</script>

Notes and troubleshooting tips: the overlay problem that hit is commonly caused by a sibling div or an absolutely positioned element with higher z-index — use the browser inspector to check element stacking, temporary background colors, or disable CSS rules to find the culprit. Prefer label + placeholder instead of relying only on placeholder for accessibility; consider this.select() on focus if replacing the default with a selection is preferable to outright clearing. Finally, ensure server-side validation does not treat the visual prompt as an actual submission.

Recommended Answers

All 4 Replies

Not two minutes after I posted this, I figured the problem out. I had an extra div tag that was covering the input box and only allowing the top portion of the box to be clicked in. I spent three hours on that one. fun stuff.

But the basic problem of clearing the text within the input box when a user clicks within it, I still don't know how to solve. If someone could help me with that one, I'd appreciate it.

please send me the code

Thanx,
sree

You want something like this? This textbox clears when the user clicks it.

<input type="text" onfocus="this.value=''; this.onfocus=null;" name="notes" value="enter ticket number here" />
commented: Good example, nice work :) +8

Thanks very much. That works great!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.