0

I have a javaScript file in which i am trying to bind a function named change by using jQuery i tried this code but it is not working?

 script.js

 for(var i=1; i<suggestionList.length; i++)
            {
                $list.append("<li>" + suggestionList[i] + "</li>");
                // attach handler for click on the suggestions
                $list.find("li").eq(i).click(function() {
                    change(suggestionList[0], suggestionList[i]);
                });
            }


    function change(changeto,changewith)
        {
            var replaceto=document.getElementById(changeto).innerHTML;
            var replacewith=changewith;
            var text1=document.getElementById("para").innerHTML;
            var afterRe=text1.replace(replaceto,replacewith);
            document.getElementById("para").innerHTML=afterRe;
        }

please help me why my code is not working?

1 Answers1

0

you can't use i inside the for-loop for click event. I have set a demo jsfiddle here .check If it helps

for(var i=1; i<suggestionList.length; i++)
{
    $list.append("<li>" + suggestionList[i] + "</li>");
     // attach handler for click on the suggestions

}

$list.find("li").click(function() {
    changewith=$(this).html();
    change(suggestionList[0], changewith);
});
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68