1

Possible Duplicate:
Find html label associated with a given input

I have following html code -

        <label for='useremail'>Registered email address</label>
        <input type='text' value='' name='useremail' onfocus='hideLabel("useremail");' id='useremail' class='useremail'>

in my webpage and below code in Javascript

function hideLabel (fieldname) {
document.getElementById(fieldname).label.style.visibility = 'hidden'; }

My intent is to hide label when user clicks on the input field. When I am testing this, I am getting following error. Is something wrong with my code?

Unhandled Error: Cannot convert 'document.getElementById(fieldname).label' to object
Community
  • 1
  • 1
JMD
  • 337
  • 4
  • 16
  • I get `TypeError: document.getElementById(...).label is undefined`. What makes you think that the `input` DOM element has a `label` property? – Felix Kling Jan 22 '13 at 00:38
  • There is an html5 property `labels` which gives a list of label elements associated with the input element. I'm not sure about support though. – Musa Jan 22 '13 at 00:44
  • @Musa: It does not seem to be supported by Firefox right now. – Felix Kling Jan 22 '13 at 00:48
  • My mistake. I think I should use code snippet at http://stackoverflow.com/questions/285522/find-html-label-associated-with-a-given-input before using .label function. You all are right – JMD Jan 22 '13 at 01:01

2 Answers2

1

I've never heard of the .label - I found this SO question about creating it: Find html label associated with a given input

Incidentally, if you include that solution in your script it will work.

Here's a jsfiddle showing it in action.

Community
  • 1
  • 1
George
  • 4,147
  • 24
  • 33
0

change your javascript to:

document.getElementById(fieldname).style.visibility = 'hidden'

NO need to put name label. After you change that give your label and id="useremail"

<label id='useremail'>Registered email address</label>
spooky
  • 1,620
  • 1
  • 13
  • 15