Click here to Skip to main content
11,412,159 members (65,642 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript PHP
Hello all,

I'm using some javascript to get the field values of a form and to check if they are empty or not:
if(document.getElementById('FIELD').value=='')
Then I'm interested on getting the associated label to that text FIELD...

It should be something like:
document.getElementById('FIELD').label

In the PHP side I've done this:
$str .= '<td class="ft1"><LABEL for="FIELD" ACCESSKEY="none" ><b style="color:#ff6d00">* THIS IS THE LABEL TEXT I WANT TO READ IN JAVASCRIPT</b></td>';
$str .= '<td class="ft2"><select name="FIELD" id="FIELD">';

I've not been able to get it working and truly I don't know where to search...

Anyone here knows it?

Thank you in advance.
Posted 9-Jun-11 7:05am
Joan Murt85.4K
Edited 9-Jun-11 8:39am
v3
Comments
aidin Tajadod at 9-Jun-11 12:42pm
   
Hi Joan, I am not PHP programmer, but If you put an ID for your label (ex lblField), you can get or set it via jQuery by $("#lblField").html()

hope this can help you.
Joan Murt at 10-Jun-11 13:08pm
   
Thank you aidin! I've used the solution that Ed has posted (see below) that is using somehow your advice. :thumbsup:
aidin Tajadod at 10-Jun-11 20:00pm
   
you are welcome.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The first part of aidin Tajadod comment is good - the easiest way for you to do this is by giving your label an Id. However, though I would strongly advise you look at doing this using jQuery before you go too far with it, if you want to do it old school you should use something like this:

$str .= '<td class="ft1"><LABEL id="Field_Label" for="FIELD" ACCESSKEY="none" ><b style="color:#ff6d00">* THIS IS THE LABEL TEXT I WANT TO READ IN JAVASCRIPT</b></td>';
$str .= '<td class="ft2"><select name="FIELD" id="FIELD">';

var LabelId = "Field_Label";
 
//This will get the innerHTML of the first child (in your case the <b> tag) if it is present, if not just the innerHTML of the label:
var LabelElem = document.getElementById(LabelId);
var LabelText = LabelElem.childNodes.length > 0 ? LabelElem.childNodes[0].innerHTML : LabelElem.innerHTML;
if(LabelText == '')
{
    //Do something
}
else
{
    //Do something different
}

In jQuery this would be something like:
var LabelId = "Field_Label";
 
//This will get the innerHTML of the first child (in your case the <b> tag) if it is present, if not just the innerHTML of the label:
var LabelElem = $("#" + LabelId);
var LabelText = LabelElem.children().length > 0 ? LabelElem.children().get(0).text() : LabelElem.text();
if(LabelText == '')
{
    //Do something
}
else
{
    //Do something different
}
This looks longer but because of the guys at jQuery, it is faster Big Grin | :-D

You should improve either of the codes above for getting the relevant text to suit your specific needs but it is a good general start.

Hope this helps,

Ed Smile | :)
  Permalink  
Comments
aidin Tajadod at 9-Jun-11 14:59pm
   
Hi EdMan, I have not tested your solution but it should work. I just did not get what is wrong with $("ID").html() ?
thanks.
EdMan196 at 9-Jun-11 15:42pm
   
jQuery uses selectors so to get elements with a particular Id you must use the CSS '#' selector thus it would need to be $("#ID") and .text() or .html() - either will do. Also, the example given contained a bold tag, thus calling .text()/.html() would return <b ...>Some text</b> rather than just the text - not what the OP wanted necessarily. Thus search for a child tag element (the bold tag in this case) and return the text of that or return text of label if there are no child elements. Hope this clears things up, Ed :)
aidin Tajadod at 9-Jun-11 16:15pm
   
Thanks EdMan, I did not notice <b...> inside the label. you are right. thanks for your time and explanations.
EdMan196 at 9-Jun-11 16:32pm
   
That's okay :) Glad I could help :)
Joan Murt at 10-Jun-11 13:10pm
   
Ed! it works wonderfully ok! I've been able to do it so now I don't have to re-parse the database to get the name translations again. (Avoiding to give access to the database to another PHP file) ^^ so it works, it is easy and it is fast. Thank you for answering so fast! :thumbsup: + 5ed!
EdMan196 at 10-Jun-11 13:11pm
   
Thanks! Good to hear to worked for you! :D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can find the label without giving it an ID by doing this:
var labels = document.getElementsByTagName('label'), l, label;
for(l in labels) {
  if(labels[l]['htmlFor'] == 'FIELD') { 
    // found it...
    label = labels[l];
  }
}

... then traverse the DOM to grab the contents of the <b> tag as EdMan196 said in his answer.
  Permalink  
Comments
EdMan196 at 9-Jun-11 14:52pm
   
Reasonable answer but extremely inefficient as soon as he has more than a few labels. Id would be far better. My 4, Ed :)
Joan Murt at 10-Jun-11 13:07pm
   
Graham, I've tried it and it works, but it takes time to create it... here to create id's is not an issue as they are created using loops and as Ed says I have plenty of them so I prefer his solution, anyway thank you very much for your rapid response. :thumbsup:
Graham Breach at 10-Jun-11 14:17pm
   
Fair enough - this approach is most useful if you can't add ids to the labels, but Ed is right that it can be an awful lot slower when there are a lot of labels to check through.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 348
1 Sergey Alexandrovich Kryukov 225
2 Sascha Lefévre 175
3 ProgramFOX 130
4 Maciej Los 110
0 Sergey Alexandrovich Kryukov 8,980
1 OriginalGriff 7,263
2 Maciej Los 3,500
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web03 | 2.8.150414.5 | Last Updated 9 Jun 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100