Francis Forget Francis Forget - 6 months ago 18
Javascript Question

Replace number in a specific path in Javascript

my question is pretty simple, but I can,t find my answer on the internet...
I have a path in an HTML file between a bracket. My path is in a for loop and contains a number, which I want to increment. Here's my code. I want to replace the "1" with my i value in my for loop.

for (var i = 0; i < comboboxesArrowDown.length; i++)
{
var comboboxArrowDown = comboboxesArrowDown[i];
comboboxArrowDown.onclick = function ()
{
var e = jQuery.Event("keydown");
e.which = 39; // Right arrow key (Keycode #39)
$("#AutresPlacements_Placements_1__Terme-Validation > .t-widget > .t-dropdown-wrap > .t-input").trigger(e);
}
}


Thank You!

Answer

Concatenate the string using the value from the loop

for (var i = 0; i < comboboxesArrowDown.length; i++)
    {
        var comboboxArrowDown = comboboxesArrowDown[i];
        comboboxArrowDown.onclick = 

        (function (val) 
        {
            return function()
            {
                  var e = jQuery.Event("keydown");
                  e.which = 39; // Right arrow key (Keycode #39)
                  $("#AutresPlacements_Placements_" + val + "__Terme-Validation > .t-widget > .t-dropdown-wrap > .t-input").trigger(e);
            }
        })(i)
    }
Comments