views:

144

answers:

5

i have a label and input outputted from the shopping cart we are using. i dont want to hack the core of the cart to change the way it outputs. so the code is thus with the input inside the label:

<div id="FormField_29" class="FormField">
    <label for="FormField_29_0"><input type="checkbox" id="FormField_29_0" name="FormField[2][29][0]" value="Yes" class="subscribeBox FormFieldOption"  /> Yes</label>
</div>

i am trying to hide the word "yes" in the label, but i cant seem to select it. its not a sibling of the input since its a label. and if i select parent not input it still makes the whole label with the input disappear. i tried next and it wont select it, since dom-wise the text isnt really next.. i cant use orphan since its not an orphan being a part of label. what am i missing? thanks!

A: 

using jquery you could remove the input element from the label, prepend it to the label (adding it back in), and then hide the label.

http://docs.jquery.com/Manipulation/remove#expr

Ty W
A: 

This works, but there is probably a better way:

var $label = $("label[for='FormField_29_0']");
var $checkbox = $label.find("input:checkbox");
$label.html($checkbox);

Edit

Since you're removing the text, I guess you can replace the label with just the checkbox:

var $label = $("label[for='FormField_29_0']");
$label.replaceWith($label.find("input:checkbox"));

Edit

Or, if you want to leave the text in place, but just hide it (again, I'm sure there's a better way):

var $label = $("label[for='FormField_29_0']");
var $text = $('<span>' + $label.text() + '</span>').hide();
var $checkbox = $label.find("input:checkbox");
$label.empty().append($checkbox).append($text);
ScottE
hopefully you don't need that label text again ;)
Ty W
A: 
var $formfield = $('#FormField_29');
$formfield.find('label').html($formfield.find('label input'));
cballou
+1  A: 

Replace the labels contents with just the checkbox.

var l = $('label[for=FormField_29_0]');
l.html(l.find('input'));
Anton
this worked great and succinct... thanks!!
liz
A: 

I think your problem has to do with the fact that your input is nested inside your label tag. Using the altered html below you should be able to alter the label's text with any number of selectors such as $('.FormField label').text('');

    <div id="FormField_29" class="FormField">
    <label for="FormField_29_0">Yes</label><input type="checkbox" id="FormField_29_0" name="FormField[2][29][0]" value="Yes" class="subscribeBox FormFieldOption"  />
</div>
ScottD
The OP said he didn't want to alter the cart output core.
Joel Potter
The problem states clearly that the HTML output cannot be modified.
Anton