A Wordpress theme allows site administrator to create forms that include a "description" for each field. The descriptions do not have unique IDs, only a common class: 'description.' They look like this:
<div class="field-head"><label for="name">Your name</label><small class="description">A unique description for this field.</small></div>
Instead of displaying these descriptions inline at each field label, as the theme does, I am hiding the description class with CSS (display:none;) and attempting to create a span that has the description as its 'title' property so that jQuery will show it as a tooltip on hover.
This is what I desire:
<div class="field-head"><label for="name">Your name</label><span class="tipwrapper" title='A unique description for this field.'>Hover for a tip</span><small class="description">A unique description for this field.</small></div>
I've successfully hidden the description field with CSS and have tried various means of adding a span before or after the element with the 'description' class. Anticipating that I will eventually iterate though each of these elements, I am first trying to succeed with just a single field (the second one with the description class.) Here are two examples of the many attempts I've made and their results:
Example attempt #1:
jQuery(document).ready(function($){
if (jQuery(('.field-head')[1]) ){
var tiptext = $('.field-head .description')[1].innerHTML;
var txt1 = '<span class="tipwrapper">'+tiptext+'</span>';
$('.field-head .description')[1].before(txt1);
};
});
RESULT: This puts the HTML in the correct place, but it is not rendered and is only displayed as inline text, like this:
<span class="tipwrapper">A unique description for this field.</span>
Example attempt #2:
(where I attempt to create the tooltip spans first, then set their titles):
jQuery(document).ready(function($){
var txt1 = "<span class='tipwrapper' title=''>Hover for a tip</span>";
$('.field-head .description').before(txt1);
});
jQuery(document).ready(function($){
if (jQuery(('.field-head')[1]) ){
var classLength = $('.field-head .description').length;
var tiptext = $('.field-head .description')[1].innerHTML;
$(('.field-head .tipwrapper')[1]).attr('title', tiptext);
};
});
RESULT: This properly renders the tooltip spans before each element with the description class, but the title attribute, where I attempt to set it, remains empty (even though adding alert(tiptext) to the function above confirms the variable is properly getting the description text. The resulting HTML is like this.
<div class="field-head"><label for="name">Your name</label><span class="tooltip" title=''>Hover for a tip</span><small class="description">A unique description for this field.</small></div>
How can I set the html from an element as the 'title' attribute of a new element before it?