Javascript Dom button onclick function working on page load why? [duplicate] - javascript

This question already has answers here:
onclick function runs automatically
(4 answers)
Closed 2 years ago.
HI i am new to js and html,
i am trying to add button on html through js and on button click i tried show alert message
here My code
topobutton=document.createElement("button");
topobutton.innerHTML="Topo";
topobutton.value="Topo";
topobutton.type="button";
topobutton.id="Topo";
topobutton.style.margin="2px";
document.body.appendChild(topobutton);
topobutton.onclick=showMap("topo");
function showMap(mapname){
alert(mapname)};
when page is reloading it shows the alert message and on button click not responding why?

EDIT
You calling the function instead of add reference / event listener.
topobutton.onclick = () => { showMap("topo") }
// OR
topobutton.onclick = function() { showMap("topo") }
// OR
topobutton.addEventListener('click', function() { showMap("topo") });

Related

Beforeunload event is not getting the custom message [duplicate]

This question already has answers here:
How can I override the OnBeforeUnload dialog and replace it with my own?
(12 answers)
Is it possible to display a custom message in the beforeunload popup?
(7 answers)
javascript onbeforeunload not showing custom message
(1 answer)
Closed 3 years ago.
I have this code :
$(window).bind("beforeunload", function() {
App.confirm('popup.cancel.confirmation.message', $('#confirm-import-modal'), true);
return "Do you really want to close?";
});
I have 2 questions about this piece of code :
I have always the message This page is asking you to confirm that you want to leave - data you have entered may not be saved., and I put the message Do you really want to close?
App.confirm show a popup, is possible to display only this popup whitout the confirm default popup from javascript ? Because now I have 2 popups, one default from javascript and another generate by App.confirm()
Thanks in advance.

jquery how to select other element that is inserted by other JS/jQuery [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 6 years ago.
I am running discourse and I am trying to modify what happens when one clicks the logout link, I do not want to modify discourse stock javascript...
There is an element elem1 that when clicked inserts another html menu with the logout link. So is it not present when $(document).ready.. is called. I need to modify what happens on elem2.click which is where the logout link is.
I want to do this...
$("#elem1").click(function() {
console.log("step1")
$("#elem2").click(function () {
console.log("step2")
..do my custom logout
})
})
but I think jquery is not finding it because it was not there when it first loaded.
Change to this
$("#elem2").on('click',function () {
console.log("step2")
..do my custom logout
})
or if you're using older jQuery:
$("#elem2").live('click',function () {
console.log("step2")
..do my custom logout
})

Can I get setTimeOut() executed after page redirection? [duplicate]

This question already has answers here:
Link to a page then execute a Javascript request
(2 answers)
Closed 6 years ago.
Why not Duplicate: Question wasn't answered in previous post.
I have a delete link, which when get clicked triggers a popup which contains a button. When I click that button, then the page (inside the popup) gets redirected to a new internal page. What I want to achieve is that the pop-up should close after 1 second after the redirection has happened. I applied the function when the button is clicked which is only available before the "Popup Redirection".
$('.page-user-cancel .btn').click(function(e) {
window.onunload = refreshParent;
setTimeout( function(){
function refreshParent() {
window.opener.location.reload();
window.close();
}
} , 1000 );
});
If I am not clear, please comment. English is not my native language.
EDIT
If that is not feasible, is it possible to trigger the function when the div on the new page (after redirection) gets loaded?
UPDATE
This isn't working:
var dival = $('.inside .alert').length;
if (dival > 0){
window.onunload = refreshParent;
function refreshParent() {
window.opener.location.reload();
window.close();
}
}
else{}
It works, only after refreshing.
Got it Worked: Had to remove the function call of refreshParent. might not be the best practice, but it is working that way.
The first part is not possible since, reloading the page doesn't keep JavaScript of the previous page running.
Second part is better. Yes, you can handle page load:
$( document ).ready(function() {
// Check your div here
});

How do i trigger a jquery event [duplicate]

This question already has answers here:
How to open a file / browse dialog using javascript?
(9 answers)
Closed 9 years ago.
i have this problem:
i have this code
if(ext!="rar") {
$('#myform').trigger('click');
}
and if the file extension of the chosen file is not rar, the file input should open the window again to choose another file, but this code does not seem to work. What should i do?
$("#btn").click(function()
{
var fu = $("#fu");
var ext = /[^.]+$/.exec(fu.val());
if (ext!="rar")
{
fu.trigger("click");
}
});
http://jsfiddle.net/tugpM/1/
Assuming that your form tag has the id myform, what you have done just triggers a click on the form (not even on the submit button of the form). Without the HTML, it is not possible to know what you are triggering the click on.
What you need to do is trigger the click on the submit button
$('#mysubmitbutton').click();
or trigger a form submit:
$('#myform').submit();

Timed alert box pop up for website? [duplicate]

This question already has answers here:
JavaScript alert box with timer
(11 answers)
Closed 9 years ago.
For my website I would like an alert box message to appear 4 seconds after the page is opened. I cant figure out how to do this as the timed function works if a button is clicked but I would like the alert box to pop up automatically withoutt any user input/button and then shut when the user clicks "okay"...
Any ideas or posts related to this topic would be great help!
Thanks
<script type = "text/javascript">
window.onload=function(){setTimeout(showPopup,4000)};
function showPopup()
{
//write functionality for the code here
}
</script>
In it's most simple form:
setTimeout(function(){alert("Hello")},4000);
4000 = 4 seconds
If you want an alert to appear after a certain about time use this code:
setTimeout(function() { alert("Your Message"); }, time);

Categories

Resources