1

What I'm trying to do here is make one function that does all the functionality for a custom select element. So I made a function that accepts three parameters which are defined in the function itself (see code below for more detail). I'm trying to accomplish the following: I want the parameters to be the IDs of the various elements (the wrapper div for example), and I want those parameters to be dropped in the function. My Code is below. Thanks so much

function createList(ParentDivID,SelectMenuID,ListMenuID) {
 $('#' + ParentDivID + "'");
 $('#' + SelectMenuID + "'");
 $('#' + ListMenuID + "'");
 var options = $("#" + SelectMenuID +'"' ).find("option");
 $(options).each(function(){
 $(ul).append('<li>' + 
 $(this).text() + '<span class="value">' + 
 $(this).val() + '</span></li>'); 
 });
 var ul = '<ul id="' + ListMenuID + "></ul>";
 $('#' + ParentDivID + "'").append(ul).end().children('#' + ListMenuID + "'").hide().click(function(){$(ul).slideToggle();});
 $("#" + SelectMenuID + '"').hide();
 }
 createList(fancySelectLarge,selectWalkerType,walkerTypeLi); 
Mutt
9356 silver badges9 bronze badges
asked May 24, 2011 at 16:18
4
  • what are fancySelectLarge,selectWalkerType,and walkerTypeLi? Are the variables that contain strings? Commented May 24, 2011 at 16:22
  • 1
    I suspect the issue is related, at least partially, with all those quotes-after-ids. Example: $('#' + ParentDivID + "'") should be $('#' + ParentDivID). Commented May 24, 2011 at 16:22
  • is fancySelectLarge a variable containing the id? or is that the actual id ? Commented May 24, 2011 at 16:23
  • Can you give some more info? What is this supposed to be doing, what is it doing and what is it not doing? Are there any errors? Commented May 24, 2011 at 16:23

8 Answers 8

7

At a guess, it is probably because your ids don't end in quote characters (which aren't allowed in ids in HTML 4), but you are appending them to the strings you are searching for with jQuery.

answered May 24, 2011 at 16:22
Sign up to request clarification or add additional context in comments.

Comments

5

You only need to do your selectors like this

$('#' + ParentDivID);

Also you need to stop interchanging 's and "s because it is causing you to miss some closing quotes

 function createList(ParentDivID,SelectMenuID,ListMenuID) {
 var options = $('#' + SelectMenuID).find('option');
 $(options).each(function(){
 $(ul).append('<li>' + 
 $(this).text() + '<span class="value">' + 
 $(this).val() + '</span></li>'); 
 });
 var ul = '<ul id="' + ListMenuID + '"></ul>';
 $('#' + ParentDivID).append(ul).end().children('#' + ListMenuID).hide().click(function(){$(ul).slideToggle();});
 $('#' + SelectMenuID).hide();
 }
 createList(fancySelectLarge,selectWalkerType,walkerTypeLi); `
answered May 24, 2011 at 16:24

Comments

3

You are messing up all of your string concatenations like:

$('#' + ParentDivID + "'"); should be $('#' + ParentDivID);

answered May 24, 2011 at 16:23

Comments

1

It's generally a bit of a mess but I've tried to fix as much as possible.

function createList(ParentDivID,SelectMenuID,ListMenuID) {
 var options = $("#" + SelectMenuID).find("option");
 var ul = $('<ul>', {id: ListMenuID});
 $(options).each(function(){
 ul.append('<li>' + 
 $(this).text() + '<span class="value">' + 
 $(this).val() + '</span></li>'); 
 });
 $('#' + ParentDivID)
 .append(ul)
 .end()
 .children('#' + ListMenuID)
 .hide()
 .click(function() { ul.slideToggle(); });
 $("#" + SelectMenuID).hide();
}
answered May 24, 2011 at 16:29

Comments

1

When you call the function, are the three parameters already variables assigned elsewhere in your code? If not, and the are actually the string id attributes, you need to enclose them in quotes.

createList("fancySelectLarge", "selectWalkerType", "walkerTypeLi"); 

Note: See other valuable responses about the incorrect quoting in $('#' + ParentDivID + "'");

answered May 24, 2011 at 16:24

Comments

0

$(ul) is undefined when execution reaches it, because var ul is only declared a few lines later on. You will also need to use document.body.createElement('ul') instead of putting '<ul ...>' in a string.

Also, the lines $('#' + ParentDivID + "'"); don't do anything.

answered May 24, 2011 at 16:22

Comments

0

You need to define ul before using it. Also, define it as $('<ul.../>') not just '<ul.../>', so that you can create a jQuery element from that definition.

answered May 24, 2011 at 16:23

Comments

0

and you want also try to create the dom element like this

$('<span class="value">') instead of a string value '<span class="value">'.

Gijs
5,2401 gold badge29 silver badges42 bronze badges
answered May 24, 2011 at 16:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.