Hi I'm developing my view in JS and I'm stuck in binding a click handler for my horizontal layout element. I've tried using Jquery
$("#myHorizontalLayout").bind("click",function(){window.alert()});
Which didn't work then I tried using attachPress
with the element which obviously didn't exist. Please help.
Update:
The JS view is the default view of the application.
Hi I'm developing my view in JS and I'm stuck in binding a click handler for my horizontal layout element. I've tried using Jquery
$("#myHorizontalLayout").bind("click",function(){window.alert()});
Which didn't work then I tried using attachPress
with the element which obviously didn't exist. Please help.
Update:
The JS view is the default view of the application.
Share Improve this question edited Nov 25, 2014 at 11:29 maniteja asked Nov 25, 2014 at 11:06 manitejamaniteja 7172 gold badges16 silver badges32 bronze badges3 Answers
Reset to default 4When on/bind
does not work, it could be that the HTML of the control has actually not been created yet at this point in time. But even if you delay the binding, the re-rendering (re-creation of the HTML after changes) would remove your listener, at least when bound on the control itself.
A proper way of doing this is using the generic attachBrowserEvent
function available on every control (here: on the layout) which internally handles all the rendering/rerendering stuff, see this example:
http://jsbin./hijutunefi/1/edit?html,output
attachBrowserEvent
works for any browser event, as it attaches a new browser event listener to the root node of the control. For the most mon browser events UI5 does event delegation, so for the "click" event and several others addEventDelegate
can also be used, as pointed out by aborjinik.
Alternatively, listening on the <body>
level with normal jQuery mechanisms should in general also work.
Which didn't work then I tried using attachPress with the element which obviously didn't exist. Please help.
Does this means that the element on which you are attaching event handler doesn't exists at this point? If this is the case you can hook the handler to some container, upper in the DOM hierarchy which you are sure that exists and filter the click events.
Example:
$("body").on("click", "#myHorizontalLayout", function(){
alert("Hey, you!");
});
As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document. For earlier versions, the .bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to .bind() occurs.
Reference here
So try replacing bind
with on
and let me know if it works or not.