1

Okay.. this is a bit hard to explain, and I was not sure how to call the title. Anyway I made a fiddle to make it more clear: see this fiddle

What do I want?

I'm working on a Wordpress plugin, and what you see in the Fiddle would be a function in the backend to add a shortcode. So kind of a shortcode generator to make it easier for the user. The user must be able to add as much field group as he wants.

What's the problem?

So.. I need to get the exact right shortcode to get the plugin work good. The shortcode will be generated with the Ready button. Now if there is only one field group, the output (shortcode) is:

metakey="< field_key:field1, title:, multiple:yes,>

which is correct.

Now if you press the 'Add' button to add another field group, and then the ready button, the output is wrong. It will output every single field twice. Like this:

metakey="< field_key:field1, title:title 1, multiple:yes, field_key:field2, title:title 2, multiple:yes,>< field_key:field1, title:title 1, multiple:yes, field_key:field2, title:title 2, multiple:yes,>

This is NOT correct, this must be something like:

metakey="< field_key:field1, title:title 1, multiple:yes,>< field_key:field2, title:title 2, multiple:yes,>

I know the problem must be somewhere in the order/set up of the 'each' functions. My idea was that the second .each functionjQuery('div.' + dataid + ' ' + '.' + getcheck).each(function () would only work in the .each function above it. This is not the case..

How to set these up right, so that the output will be as the above?

So this problem might be somewhere in here:

jQuery('.filter-checkbox:checked').each(function () {
    dataid = jQuery(this).data('id');
    getchecked = jQuery(this).val();
    getcheck = jQuery(this).data('rel');

    val += getchecked + '="';
});

jQuery('.' + dataid).each(function () {
    val += '<';

    jQuery('div.' + dataid + ' ' + '.' + getcheck).each(function () {

But maybe some changes in the HTML can also work, not sure.

Hope my problem is clear. Thanks in advance.

Trekdrop
  • 475
  • 7
  • 27

1 Answers1

1

Change line

jQuery('div.' + dataid + ' ' + '.' + getcheck).each(function () {

to:

jQuery(this).find('.' + getcheck).each(function () {

See: http://jsfiddle.net/bTER5/60/

Kabie
  • 10,489
  • 1
  • 38
  • 45