javascript - Saving the clicked element and accessing it from another object -


what set click event on particular set of dom elements.

i remember element has been clicked may need retrieve information later.

i able access js object if possible.

below have far...

<ul>     <li>item 1</li>     <li>item 2</li>     <li>item 3</li> </ul> <button>       have clicked on? </button>  <script> var listitem = {     elementhandle: $('li'),     instanceclicked: '',     bindeventhandlers: function(){         $(this.elementhandle).click(this.alertthecontent);     },     alertthecontent: function(){         this.instanceclicked = $(this);         alert(this.instanceclicked.html());     } }  var button = {     elementhandle: $('button'),     bindeventhandlers: function(){         $(this.elementhandle).click(this.showlistitemclicked);     },     showlistitemclicked: function(){         console.log(listitem.instanceclicked);     } }  listitem.bindeventhandlers(); button.bindeventhandlers(); </script> 

i think close console log on showclickeditem() function returning ''. when created listitem object.

please find fiddle here https://jsfiddle.net/rbfaxw12/

you need use listitem instead of this in alertthecontent function this refers current element context invoked click handler i.e. li not listitem

alertthecontent: function(){     listitem.instanceclicked = $(this);     alert(listitem.instanceclicked.html()); } 

demo


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -