6

Okay, so I've had this code that used to work just fine. I recently upgraded my jQuery from 1.4.4 to 1.5.2, and apparently this has quit working. However, I've tried the code with 1.4.4. and 1.3.2 and it won't work there, either.

This did work. I can't figure out why it isn't. Any help?

Edit: start and end are arguments, with the text of the select element's ID.

 var selectedIndex = document.getElementById(start).selectedIndex; // get the selected index from the correct select box if (selectedIndex != -1) { // if something is selected, do the following: var selectedElement = document.getElementById(start).options[selectedIndex]; // get the element that's selected if (selectedIndex == (document.getElementById(start).options.length - 1) && selectedIndex != 0) { selectedIndex--; // if we're at the bottom of the list, set our selectedIndex variable to the one right before it } $("#" + start).remove(selectedElement); // remove the selected element from the start side $("#" + end).append(selectedElement); // and add it to the end of the ending side } 

Here's an example of an option I want to move.
<option sortable="yes" datatype="string" value="foo" type="arbitrary">Foo</option>

The issue I'm getting is apparently within jQuery itself - using the full version,
expr.replace is not a function [Break On This Error] expr = expr.replace( /\=\s*([^'"]])\s]/g, "='$1']" ); [jquery-latest.debug.js, line 4540]
The error happens when I hit the $.remove portion of the code.

Thanks.

2
  • Why do you say that doesn't work. I mean, what is the error? Commented May 2, 2011 at 15:12
  • The HTML is dynamically generated. Updated OP with both an option and the error. Can't believe I forgot to mention what the issue actually is. Commented May 2, 2011 at 15:13

3 Answers 3

3

You are getting this error because .remove() takes a string selector, and if none is supplied, removes those of the parent object. Try

$(selectedElement).remove(); 
Sign up to request clarification or add additional context in comments.

3 Comments

But why did this used to work? That's what I'm confused on. This used to work fine - I've added an ID to the element I'm trying to remove and passed that in the arguments for .remove() but instead of erroring, it just does nothing. I'll give this a shot.
Couldn't we just use $(selectedElement).remove(); ?
This is it. Gah. Thanks. I just have no idea why this used to work and has subsequently stopped, in all versions of jQuery.
2

Is there a reason you're using the getElementById calls? Let jQuery do the work for you... Instead of using:

$("#" + start).remove(selectedElement); // remove the selected element from the start side $("#" + end).append(selectedElement); // and add it to the end of the ending side 

}

try using:

$("#"+start+" option:selected").appendTo("#" + end); 

It combines the remove/append operations into one, and may solve your problem.

1 Comment

The reason for the getElementById calls is to get certain values out, like the selected index. This was an early-ish jQuery project of mine, and I wasn't really using the filters like that. I'll also give this a shot.
0

Unfortunately, <option> is not ordinary element so it might cause such problems.

See the accepted answer on this question for code that should work.

4 Comments

Yeah, <option> has been a pain, especially in dealing with the differences between IE and every other browser - I'm just confused why this stopped working, when it used to.
@tjs good question.. did you try the code in the other question?
Haven't used that specifically, but the accepted answer here was very similar and got me going. I just have no idea A) why this stopped working, and B) why no one else discovered this before me (where it's being used).
Yep, nope. No idea. I know for a fact it worked for months, and I don't know at what point it broke.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.