Weel, it's "simple" (easy to say after a couple of tries, I must
admit). The real reason is in the REGEXP, which does not work at all
for numbers, but only on empty []. So, the FIRST run of the script
works in both cases, since the [] are empty. But then, on the second
run, the REGEXP is failing miserably... only on the "broken" code (the
div productOption AFTER the #newProductOption div). Why ? Because
starting at the 2nd run, the "var tpl = JQ('.productOption:first')"
line is now retrieving, in this broken situation, the FIRST
created .productOption div, with a name of [1000]. Since the REGEXP is
failing, it does not match anything, so it does not CHANGE anything...
your cloned node remains the same, cloning after cloning.
On the working solution, of course, you always select the always-first
div that does not contain an number in its name, so the REGEXP still
matches. Try to put [2000], you'll see it's failing.
But, overall, I think your code is complex: why take :first, if your
template div is the last ? Putting a more logical (to me) last would
work like a charm.
It took me a bit of time because initially, I thought that you would
extract the number from the name, increment it, and write it to the
new node. But with the way your algorithm works (much simpler than my
first though), why not simply create a node on the fly, with JQ('my
html code [' + OINDEX + '] blabla') ?

Best,

Order of divs make the jQuery selector retrieve different

On Aug 25, 4:36 pm, Stephan Beal <[EMAIL PROTECTED]> wrote:
> On Aug 24, 3:10 pm, Scott Sauyet <[EMAIL PROTECTED]> wrote:
>
> >         var new_name = n.replace(/\[\d?\]/, '[' + OINDEX + ']');
>
> For your code you don't need the \d?, because your template doesn't
> have a number in it. That said, your template code is incorrect:
>
> JQ(tpl).find('[EMAIL PROTECTED]').each(function(){
>
> Carefully count the [ and ] characters in that string and you'll find
> that they're mismatched and cannot match your regex:
>
> var new_name = n.replace(/\[\d?\]/, '[' + OINDEX + ']');
>
> Also, it wouldn't surprise me if you need to escape the [ and ] inside
> the find() command, as detailed in the FAQ:
>
> http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_select_an_...
>
> But apparently your working example demonstrates that that's not
> necessary. THAT i can't explain, though.
>
> :)

Reply via email to