Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Loop not working for multifield

Avatar

Avatar
Validate 10
Level 2
Shaheena_Sheikh
Level 2

Like

1 like

Total Posts

87 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Give Back
Boost 1
Applaud 5
View profile

Avatar
Validate 10
Level 2
Shaheena_Sheikh
Level 2

Like

1 like

Total Posts

87 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Give Back
Boost 1
Applaud 5
View profile
Shaheena_Sheikh
Level 2

14-04-2021

$(".coral3-Multifield-item").each(function( index ) {}

this loop is not working for the newly added items.

 

$("coral-multifield").children("button[coral-multifield-add]").on("click", function() {

       $(".coral3-Multifield-item").each(function( index ) {
             console.log("add item");

        })});

if I already have 2 items in my multifield when I open the dialog, then the above console log prints 2 times. Now if I Add a new item to the multifield (3rd item), the console log still works for the first 2 items only, though it should have worked for 3 times.

log:

add item

add item

Now if I add another item (4th item), then the log shows 3 times and twice but doesnt show for the 4th item:

add item

add item

add item

 

add item

add item

add item

Now if I save my dialog changes (total item=4) and open the dialog again, and Add a new item, this time console log prints 4 times and doesn't print for the the 5th item.

add item

add item

add item

add item

If there are no items when I open the dialog, and I add a new item, then the log doesn't print at all.

 

I need the run the loop everytime a new item is added.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Affirm 100
Level 10
asutosh_jena
Level 10

Likes

373 likes

Total Posts

480 posts

Correct Reply

133 solutions
Top badges earned
Affirm 100
Ignite 1
Establish
Give Back 50
Give Back 5
View profile

Avatar
Affirm 100
Level 10
asutosh_jena
Level 10

Likes

373 likes

Total Posts

480 posts

Correct Reply

133 solutions
Top badges earned
Affirm 100
Ignite 1
Establish
Give Back 50
Give Back 5
View profile
asutosh_jena
Level 10

14-04-2021

Hi @Shaheena_Sheikh 

 

Adding the below code will execute only on button click and the loop also works as expected. This will run each time a new item is added i.e. each time you click on add button.

 

(function (document, $, ns) {
"use strict";

$(document).on("click", ".coral3-Button--secondary", function (e) {
e.stopPropagation();
e.preventDefault();

//Code from here
$(".coral3-Multifield-item").each(function (index) {
console.log("add item");
})
});

})(document, Granite.$, Granite.author);

Hope this helps!

 

Thanks! 

Answers (1)

Answers (1)

Avatar

Avatar
Validate 1
Level 6
KiranVedantam1992
Level 6

Likes

150 likes

Total Posts

155 posts

Correct Reply

44 solutions
Top badges earned
Validate 1
Establish
Give Back 3
Give Back
Boost 50
View profile

Avatar
Validate 1
Level 6
KiranVedantam1992
Level 6

Likes

150 likes

Total Posts

155 posts

Correct Reply

44 solutions
Top badges earned
Validate 1
Establish
Give Back 3
Give Back
Boost 50
View profile
KiranVedantam1992
Level 6

14-04-2021

Hi @Shaheena_Sheikh,

 

What are you trying to do with the multifield? It is recommended to use Models to handle the business logic.

 

Thanks,

Kiran Vedantam.