Solved

jquery: search for first occurence of <label> tag that is in parents()

Posted on 2013-11-16
5
779 Views
Last Modified: 2013-11-17
Hi,

I'm wondering how I can find the first occurrence of the <label> tag that is in .parents().

For example, I have this code:

 var oSelected = $('form select option:selected');

And suppose I have a <label> html that occurs before the <select><option> but I may have a <span> or a <div> between the <label> and <select><option> tags. How would find the <label> from the variable oSelected using jQuery? Then I need to find the value of the "for" attribute of this <label> tag.

I have the $selected in a loop using .each() like so:

oSelected.each(function(index, value) {
   oSelectedValues['title'] = $(this).parent().prev().attr("for");
   (more code)
});

I have the above code that works now but it only works IF I have the <label> right before the <select>. If there is anything in between the <label> and the <select> it won't work. For example, if I have <label></label><span></span><select><option></option></select> it won't work.

Thanks
0
Comment
Question by:Victor Kimura
5 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 125 total points
ID: 39653548
What is the real markup of your code, an example layout is no good if it is nothing like your real markup. e.g. for your example above you could just do

$(this).prev().prev().attr("for")
0
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 125 total points
ID: 39653739
Hi,
as long as you have no container element like DIV or SPAN around your label / select box combination, it will be hard (to impossible) to create a selector for the right element.

And as GaryC123 already mentioned, please attach the real DOM / HTML source.

So if you have a structure like
<div><label/><span/><select/>

Open in new window

then this selector is able to get the element
$(this).closest("div").find("label")

Open in new window


HTH
Rainer
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 125 total points
ID: 39653893
Agree with the others. Without seeing your document, can't really be specific, but if the label that you're looking for is linked to the SELECT, then the 'for' attribute will be the same as the ID of the SELECT- why not just grab the ID of the select.
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 125 total points
ID: 39653995
<label for="myselect"></label><span></span><select id="myselect"><option></option></select>

test page : http://jsfiddle.net/HUC5x/

var oSelected = $('form select option:selected');
var selectID = oSelected.parent().attr("id");
var label = $("label[for='" + selectID + "']");

//checking
alert( label.text() );

Open in new window

0
 

Author Closing Comment

by:Victor Kimura
ID: 39655438
Thank you, fellows. All that info helped. I was just reading the doc on .closest and I forgot about this function.
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question