1

Hi I am looking to find HTML contained on the page and replace it using jQuery.

I have spent hours looking at similar questions on here but I couldn't find any that achieve exactly what I am looking for.

I have the following HTML on my page

<div id="mCSB_1_container" class="mCSB_container" dir="ltr" style="position:relative; top:0; left:0;">
<label class="">
<input type="checkbox" value="blazers">
<span>
Blazers & Waistcoats
<span class="prdctfltr_count">6/8</span>
</span>
</label>
<label class="">
<input type="checkbox" value="coatsjackets">
<span>
Coats & Jackets
<span class="prdctfltr_count">1/3</span>
</span>
</label>
  </div>

And I need a way to say find the following code

<label class="">
<input type="checkbox"

and replace it with

<option class=""

So far all the other questions I have looked at seem to replace the entire tag which loses the value="" attribute I need to keep.

Any assistance would be appreciated.

Edit: I need to end up with something along the lines of the code below but I'm taking it one step at a time

<div id="mCSB_1_container" class="mCSB_container" dir="ltr" style="position: relative; top: 0px; left: 0px;">
<select>
<option class="" value="blazers">
Blazers & Waistcoats
</option>
<option class="" value="coatsjackets">
Coats & Jackets
</option>
</select>
</div>

6
  • 2
    You should replace the entire tags. i.e. replace the existing items, but you should copy across any properties you want to keep for each element. Commented Aug 26, 2015 at 13:35
  • So you want to create a <select> out of your current html? Commented Aug 26, 2015 at 13:39
  • @Stryner yes that's the ultimate aim, I have added the code structure I eventually need to achieve to the original question. Commented Aug 26, 2015 at 13:43
  • find span and append its input to a select box Commented Aug 26, 2015 at 13:44
  • @TrueBlueAussie I did think it might end up having to be something like that. The problem I have is that I can't figure out how to copy the existing properties across for each element. Every question I have come across so far seems to just replace the entire tag without any properties. Commented Aug 26, 2015 at 13:45

1 Answer 1

2

You should be able to do it with something like this:

var $select = $('<select>');
$('#mCSB_1_container').append($select);
$('#mCSB_1_container label').each(function () {
    var $label = $(this);
    var $input = $label.find('input');
    var value = $input.val();
    var text = $label.text();
    $('<option>').val(value).text(text).appendTo($select);
    $label.remove();
});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/yekxrca5/3/

This one is a little long-winded for explanation and can obviously be shortened.

The end result looks like this (reformatted as white-space is not removed):

<div id="mCSB_1_container" class="mCSB_container" dir="ltr" style="position:relative; top:0; left:0;">
   <select>
       <option value="blazers">Blazers &amp; Waistcoats 6/8</option>
       <option value="coatsjackets">Coats &amp; Jackets 1/3</option>
   </select>
</div>
  • You could of course trim all white-space from the text first.
  • You also probably want to add a name="" attribute to the select so that you get a selected value posted back to the server.

JSFiddle: http://jsfiddle.net/TrueBlueAussie/yekxrca5/4/

And if you do want to retain the classes from the labels, use:

$('<option>').val(value).attr("class", $label.attr('class')).text($.trim(text)).appendTo($select);

JSFiddle: http://jsfiddle.net/TrueBlueAussie/yekxrca5/5/

Update:

If you want to retain the current check box controls, don't remove() them, just hide() them (they are shown for demonstration only) and update them as the selection changes:

// update the checkboxes to match the selection
$select.change(function(){
    var selection = $(this).val();
    // Now find the matching checkbox by value and change selection
    var $cb = $(':checkbox[value="'+selection+'"]').prop('checked', true);
    $(':checkbox').not($cb).prop('checked', false);
}).change();

JSFiddle: http://jsfiddle.net/yekxrca5/8/

The change() at the very end just triggers a change event at load time, so that the current selection is ticked.

Sign up to request clarification or add additional context in comments.

13 Comments

And OP wants to keep attribute name (and value), 'class' in this case, so this could help, probably: stackoverflow.com/questions/2048720/… Not sure that JQuery can handle it...
@nevermind: An empty class is pointless, but I will investigate further what the aim is. Cheers :)
Yes, you are right... also, 'class' is pretty pointless on 'option' tag anyway... But, i thought that 'class' is just example, that OP wants to get ANY attribute (can be class, alt, title... whatever...that 'dynamic' attribute is in question), but, that was just, obviously, editing mistake. Cheers! :)
@TrueBlueAussie brilliant thank you for this, it comes out just as I wanted. The blank class was simply because I was looking to retain a similar structure to before. But at present it's not needed. Thank you for going above and beyond what I was expecting.
@TrueBlueAussie The only issue is that when you actually select a value it doesn't load anything. As you mentioned I need to pass the selected value to the server. It might be a bit of an ask but is there anyway to do this based on the original structure that was using checkboxes? I want to keep the same functionality of the original checkboxes but just styled in a select drop down box if that makes sense.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.