ilan Expert Licensed User Longtime User Sep 14, 2021 #1 hi i am working a very big project with b4j and jserver. i am creating dynamically html elements and update the inner HTML of the relevant div in my webpage. i am using for that specific page a WebSocket now my question is this. i need to trigger several events for some dynamically created elements like _click, _changed ... i cannot add the click event for the whole div and use the target element because it will break other features in my app so what i do now is adding js code dynamically to those elements like a click listener with ws.Eval() function and a callback to a b4j event. is this a good approach? is there a better way to do that? example: B4X: Dim js As String = $" $(".menuitem").click(function() { let itemid = jQuery(this).attr("itemid"); let itemtype = jQuery(this).attr("itemtype"); b4j_raiseEvent("remove_Item",{id:itemid,type:itemtype}); }); "$ ws.Eval(js,Null) b4j event: B4X: Sub remove_Item(Params As Map) Log(Params.Get("id")) Log(Params.Get("type")) End Sub everything works fine, i am just wondering if this is the right approach or maybe i should do it in a different way? thanx
hi i am working a very big project with b4j and jserver. i am creating dynamically html elements and update the inner HTML of the relevant div in my webpage. i am using for that specific page a WebSocket now my question is this. i need to trigger several events for some dynamically created elements like _click, _changed ... i cannot add the click event for the whole div and use the target element because it will break other features in my app so what i do now is adding js code dynamically to those elements like a click listener with ws.Eval() function and a callback to a b4j event. is this a good approach? is there a better way to do that? example: B4X: Dim js As String = $" $(".menuitem").click(function() { let itemid = jQuery(this).attr("itemid"); let itemtype = jQuery(this).attr("itemtype"); b4j_raiseEvent("remove_Item",{id:itemid,type:itemtype}); }); "$ ws.Eval(js,Null) b4j event: B4X: Sub remove_Item(Params As Map) Log(Params.Get("id")) Log(Params.Get("type")) End Sub everything works fine, i am just wondering if this is the right approach or maybe i should do it in a different way? thanx
Erel B4X founder Staff member Licensed User Longtime User Sep 14, 2021 #2 It can be simplified a bit by calling b4a_addEvent (see b4j_ws.js), though your code looks fine. Upvote 0
ilan Expert Licensed User Longtime User Sep 14, 2021 #3 Erel said: It can be simplified a bit by calling b4a_addEvent (see b4j_ws.js), though your code looks fine. Click to expand... thanx erel, so you advice changing addEvent then raiseEvent. thank you ? Upvote 0
Erel said: It can be simplified a bit by calling b4a_addEvent (see b4j_ws.js), though your code looks fine. Click to expand... thanx erel, so you advice changing addEvent then raiseEvent. thank you ?