run one javascript function for different element - javascript

how run one javascript function for diffrent element with one ASP.Net button click ?

for example if you want to run another button's click event function you can call document.getElementById('myButton').click();
but remember, if click function of that button is doing postback, you need to prevent it. because that calling that function will avoid the code from continuing.

Related

How to call anonymous functions from HTML components' events using JQuery?

I have a button. I registered a function to treat the click event. However, I want to call it if another event happens. How do I call it from another asynchronous (or synchronous) event?
My first idea was to write the function with a name. That way, I can bind it to the click event to my button and also call it whenever I want to. However, in the case I wrote an anonymous function, what are my options?
Example (pseudo-code):
create button b;
assign some function f to b to execute on click;
b.invoke(args); //invoke runs f without the need to click
You can fire the "click" event on the element.
$('#MyButton').trigger('click');
EDIT: sorry, I misunderstood the question. But, you can still trigger the event and let the function to use some global variable so you can access everywhere.

Send click event on button loaded dynamically

So this is slightly different than all the posts I have found on the subject. I have a button that gets loaded dynamically via Jquery, I save the selector of this button and later on in my code I need to send a click event (Emulate someone clicking on the button) Now normally I would just use $('#myID').click();
and this casts a click. But Since my button is loaded dynamically this does not work. Now I do NOT need to handle the onclick event. I could use
$(document).on('click', '#myId',function(e){});
for that. I need to actually send the click event. I have tried
.click();
.on('click);
.onClick();
.trigger('click');
Any ideas?
You could also breakout the code that you want to happen when you click on the button into a function if it's simple enough and instead of trying to fire a click event just fire the function the button normally fires.
By using setTimeout() to call the function again and again you are essentially polling the element, untill it actually exists, which is when you fire the click event.
// Wait for everything in the document to be loaded
jQuery(document).ready(function() {
// Make the initial call to the function
fire_click('#myID');
// This function tries to find the button, and if it can't
// find it, it calls itself again in 50 ms.
function fire_click(selector) {
elem = jQuery(selector);
if (elem.length == 0)
setTimeout(fire_click, 50);
else
elem.click();
}
});
A better solution would be to have a callback function that is fired when the button is loaded. This callback function can then fire the click event on the button, since the callback function is only called when the button is actually there. Generally it's a good idea to avoid polling for information when you can, so therefore this would be considered a better solution.

calling js function works only for the first time on jquery mobile

on jquery mobile web app I'm calling js function on close button. That js function close it's callers div parent.
That works fine, but problem is that I have multiple close buttons and this function works perfectly first time,
after that onclick doesnt work. It doesnt enter into js function.
I tried to put js function at very bottom of my _Layout.cshtml page but it doesnt change anything.
update
<script type="text/javascript">
$('#closeTable').click(function () {
$(this).parent().hide();
});
</script>
<div id="closeTable"></div>
Your problem is originating from the fact that you're using an ID to add event listeners to. In your JS, you have this line:
$('#closeTable').click(function () { ...
This line attaches a click event handler to the div with ID closeTable. Since there can only be one element with this ID, once it's hidden the user can't click it again and so the function won't be executed again.
If you have multiple close buttons as you say, you should instead use a class selector to attach handlers:
$(".closeTable").click(function() { ...
This will instead attach a listener to every element with class closeTable. This means that when any of them are clicked the function will execute, so it will work multiple times.
Hope this helps.

Javascript function call in asp.net page

I have taken 1 textbox, 1 button and 1 alert image in my asp.net page. And I have written JavaScript function and call it in onblur and onclick event of text box. The function of onblur is that if the textbox is empty then the alert picture will show else picture will hide. And in onclick picture will hide. And another JavaScript function I also used to hide the picture when I open the page first time. I call this in pageload of aspx.cs page
Same things I want to do in my button Onclientclick event but it is not working because when I clicked on button the page is loaded.
How to solve this problem?
Your button click is causing postback. The simple way without seeing your code, at the end of javascript function that onclick is invoking, before the closing bracket - return false.
From the code you shared, do return false after alert();

Linking html element to jQuery.click() using attribute "title"

I have a html code:
<a title="intro">INTRO?</a>
I need to link a jQuery click event on the tag. Using the solution given here I wrote the following javascript:
jQuery("a[title='intro']").click(alert("abc"));
However the page is alerting ("abc") on page load rather than on clicking the tag. Also to inform that the above code is NOT inside the load function jQuery(function() {... } and is a separate function.
Any solutions pls?
You are invoking the alert function during the event registration and is passing the value returned by the alert as the click callback handler.
Instead you need to pass a function reference as the click callback and within the function you can add the alert call
jQuery("a[title='intro']").click(function(){
alert("a")
});

Categories

Resources