I am trying to find all attributes of an option tag (except the value) so I can replace the option tag with a list item tag and then add the attributes to the list item. Every Google result I come across says to use replace()
to do this, however, I am getting the following error in console:
Uncaught TypeError: undefined is not a function
This occurs on the following line:
var elemAtts = $(this).replace('<option value="'+$(this).prop("value")+'" ', '');
This is the entire script:
$('select').each(function(){
var selectName = $(this).prop("name");
var selectOpts = $(this).find("option");
var selectPar = $(this).parent();
var hiddenInput = '<input type="hidden" name="'+selectName+'" value="">';
var menu = '<div class="jqSelect">' +
'<div class="selected-text" id="'+selectName+'-selected"><div id="'+selectName+'-text">'+$(this).find(":selected").text()+'</div>' +
'<div class="caret" onclick="$(\'ul#'+selectName+'-menu\').toggle();"></div>' +
'<ul id="'+selectName+'-menu">';
selectOpts.each(function(){
if($(this).is(":selected")){
$('#'+selectName+'-selected').text($(this).text());
menu += '<li class="selected"';
} else {
menu += '<li';
}
var elemAtts = $(this).replace('<option value="'+$(this).prop("value")+'" ', '');
elemAtts = elemAtts.replace('>', '');
menu += ' data-option-value="'+$(this).prop("value")+'" '+elemAtts+'>'+$(this).text()+'</li>';
});
menu += '</ul></div>';
selectPar.html(menu + hiddenInput);
$('ul#'+selectName+'-menu').hide();
generatedMenu = $('#'+selectName+'-menu');
generatedMenu.find('li').each(function(){
$(this).on('click', function(){
$('#'+selectName+'-text').text($(this).text());
$('#'+selectName+'-menu').hide();
$('input[name="'+selectName+'"]').attr("value", $(this).data('option-value'));
$(this).siblings().removeProp('class');
$(this).prop('class', 'selected');
});
});
});
Basically what I am trying to do is turn this...
<option value="123" rel="toggleElem" class="foo" id="bar">Hello!</option>
Into this(string form)...
rel="toggleElem" class="foo" id="bar"