javascript - creating new element with click-function do not work (cant set focus) -


If I create a new element with jquery and add a click function as above, then the click function is definitely fixed Does not work from

I want to set focus on an equality element, but it only focuses on the previous one.

To make a better explanation, I created a JSFIDEL

  var newUL = $ (' & gt;', {text: "ul text", 'Class': 'errorListUl'}); (Var i = 0; error list [i]; i ++) {var el = errorList [i]; Console.log ("element:", el.element); $ ("
  • ", {"class": "errorListLi", html: "& lt; span & gt;" + Error list [i] .element.name + "& lt; / span & gt; & lt; span & gt; "+ Error list [i] .method +" & lt; / span & gt; "+ Error list [i]. Message, click: function () {$ (el. Element. Focus (); console.log ("Jump to:", $ (el .element))}}}. Annex (neuol); } NewUL.appendTo ($ ('# response'));

    for your before the loop ends That is inserted into your element DOM. Due to this fact, it will only add operators to the last element. To handle this problem, you need an anonymous function (closing).

      var newUL = $ ('& lt; ul / & gt;', {html: "click on the error message (L) focuses on the equivalent element screaming. Set is the last element. 

    ", 'class': 'errorListUl'}); (Var i = 0; error list [i]; i ++) {(function (_i) {var el = errorList [_i], htmlText = "& lt; span & gt;" + el.element.name + " & lt; span & gt;" + el.method + "" + el.message; console.log ("element:", el.element ); $ ("& Lt; li / & gt;", {"class": "ErrorListLi", html: htmlText, click: function () {$ (el.element). Focus (); console.log "Go to:", $ (el.element)}}}). AppendTo (NewUl);}) (i)} newUL.appendTo ($ ('# response'));

  • Comments

    Popular posts from this blog

    Java - Error: no suitable method found for add(int, java.lang.String) -

    java - JPA TypedQuery: Parameter value element did not match expected type -

    c++ - static template member variable has internal linkage but is not defined -