1
<div id="container_security" class="ui-dialog-content ui-widget-content" style="display: block; width: auto; min-height: 0px; height: 485.40000009536743px;">
        <div class="container_sec_class_container" style="display: block;">
            <div class="cont_sec_sec" style="height: 70px;">
                <p class="container_title">
                    Name
                </p>
                <input id="container_name" readonly="readonly" type="text">
                <p class="container_secclass_title">Security Class</p>
                <dl class="container_security_class dropdown">
                    <dt>
                     <span class="parent">
                      <span class="container_secclass_dropdown">Shipping and Receiving</span>

In the above code I want to set the title attribute to the bottom most element of above DOM tree. I want to set titles of all child spans under span whose class is parent.

i m trying the code written below but it is not working

var dropDownItem = $('#container_security > div > dl > dt > span > span');
dropDownItem.attr('title', dropDownItem.val());
Abhi
  • 5,501
  • 17
  • 78
  • 133

5 Answers5

3

I'd suggest using a descendant selector (rather than a brittle series of child selectors) with its class:

var dropDownItem = $('#container_security .container_secclass_dropdown');
dropDownItem.attr('title', dropDownItem.val());

But note that val doesn't apply to span elements (only form fields), so the dropDownItem.val() part doesn't make sense. Perhaps you wanted .text().

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Try this:

$spanelements = $("#container_security .container_secclass_dropdown");
$spanelements.attr("title", $spanelements.text());

Code Explanation:

It pretty much selects the div with the id of container_security then looks down and down for it's descendand and then when it finds an element with the class of .container_secclass_dropdown it then selects that element and then it sets it's attribute.

This question might help you with what type of selector to choose.

This question might help you in choosing .text() or .val()

Community
  • 1
  • 1
Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113
1

Try this one only if you want to play with last span

$('span:last').attr({title:$('span:last').text()})

or if with all parent classes spans, then use

$(document).ready(function() {
      $('span.parent').children().each(function() {
         $(this).attr({title:$(this).text()});
      });
});
Manish Jangir
  • 5,329
  • 4
  • 42
  • 75
1

may be this: as you said that you want to set title to all span under span whose class is parent

$('.parent span').attr("title","Temp Title");

if you have more than one span, you try the following..

$('.parent span').each(function() { 
    $(this).attr("title",$(this).html());
});
patel.milanb
  • 5,822
  • 15
  • 56
  • 92
0

This code selects all the elements at the Bottom-most level of the DOM tree so in this case the bottommost span is selected.

CODE

$children = $("body").children();
while ($children.children().length > 0) {
    $children = $children.children();
}
//Now the variable $children is the set of elements at the bottom-most level of DOM.
console.log($children.attr('title', "bottom-most element in DOM tree"));

Fiddle demo

Rajesh Paul
  • 6,793
  • 6
  • 40
  • 57