Div element appearing only for seconds
I am trying to make a table appear when I click on a submit button in a form.
I am using JavaScript for that function.
See code below:
<script type="text/javascript">
function showTable1234() {
var showtab = document.getElementsByClassName('container12');
for(var i =0;i !=showtab.length;i++)
showtab[i].style.display = 'block';
}
</script>
Here is the code for the button which is calling the above function:
<button type="submit" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
This is div element which is to be shown on clicking the submit button:
<div class="container12" >
<h2>Hover Rows</h2>
<table class="table table-hover" id = "tab12">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
.....
Now as soon as I run the code on the Chrome browser and clicking on submit button, table is visible only but only for mini second or something and the disappears.
It appears again when i click on submit button but again for same amount of time.
I even tried with this code but same result
<script type="text/javascript">
function showTable1234() {
var showtab = document.getElementsByClassName('container12')[0];
for(var i =0;i !=showtab.length;i++)
showtab[i].style.display = 'block';
}
</script>
But for this i am getting this error
Uncaught TypeError: Cannot read property 'style' of undefined
at showTable1234
Advance Thanks for the help.
javascript html
add a comment |
I am trying to make a table appear when I click on a submit button in a form.
I am using JavaScript for that function.
See code below:
<script type="text/javascript">
function showTable1234() {
var showtab = document.getElementsByClassName('container12');
for(var i =0;i !=showtab.length;i++)
showtab[i].style.display = 'block';
}
</script>
Here is the code for the button which is calling the above function:
<button type="submit" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
This is div element which is to be shown on clicking the submit button:
<div class="container12" >
<h2>Hover Rows</h2>
<table class="table table-hover" id = "tab12">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
.....
Now as soon as I run the code on the Chrome browser and clicking on submit button, table is visible only but only for mini second or something and the disappears.
It appears again when i click on submit button but again for same amount of time.
I even tried with this code but same result
<script type="text/javascript">
function showTable1234() {
var showtab = document.getElementsByClassName('container12')[0];
for(var i =0;i !=showtab.length;i++)
showtab[i].style.display = 'block';
}
</script>
But for this i am getting this error
Uncaught TypeError: Cannot read property 'style' of undefined
at showTable1234
Advance Thanks for the help.
javascript html
Please click edit then click the[<>]
snippet editor and create a Minimal, Complete, and Verifiable example
– mplungjan
Nov 22 '18 at 13:37
1
One error is that you submit the page so you will never see the result - change the button to a button<button type="button" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
or use AJAX
– mplungjan
Nov 22 '18 at 13:37
1
The other error is that this is ONE tabdocument.getElementsByClassName('container12')[0];
and cannot be looped over. Remove the[0]
– mplungjan
Nov 22 '18 at 13:39
Thanks for the answer
– Atul
Nov 22 '18 at 14:55
add a comment |
I am trying to make a table appear when I click on a submit button in a form.
I am using JavaScript for that function.
See code below:
<script type="text/javascript">
function showTable1234() {
var showtab = document.getElementsByClassName('container12');
for(var i =0;i !=showtab.length;i++)
showtab[i].style.display = 'block';
}
</script>
Here is the code for the button which is calling the above function:
<button type="submit" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
This is div element which is to be shown on clicking the submit button:
<div class="container12" >
<h2>Hover Rows</h2>
<table class="table table-hover" id = "tab12">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
.....
Now as soon as I run the code on the Chrome browser and clicking on submit button, table is visible only but only for mini second or something and the disappears.
It appears again when i click on submit button but again for same amount of time.
I even tried with this code but same result
<script type="text/javascript">
function showTable1234() {
var showtab = document.getElementsByClassName('container12')[0];
for(var i =0;i !=showtab.length;i++)
showtab[i].style.display = 'block';
}
</script>
But for this i am getting this error
Uncaught TypeError: Cannot read property 'style' of undefined
at showTable1234
Advance Thanks for the help.
javascript html
I am trying to make a table appear when I click on a submit button in a form.
I am using JavaScript for that function.
See code below:
<script type="text/javascript">
function showTable1234() {
var showtab = document.getElementsByClassName('container12');
for(var i =0;i !=showtab.length;i++)
showtab[i].style.display = 'block';
}
</script>
Here is the code for the button which is calling the above function:
<button type="submit" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
This is div element which is to be shown on clicking the submit button:
<div class="container12" >
<h2>Hover Rows</h2>
<table class="table table-hover" id = "tab12">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
.....
Now as soon as I run the code on the Chrome browser and clicking on submit button, table is visible only but only for mini second or something and the disappears.
It appears again when i click on submit button but again for same amount of time.
I even tried with this code but same result
<script type="text/javascript">
function showTable1234() {
var showtab = document.getElementsByClassName('container12')[0];
for(var i =0;i !=showtab.length;i++)
showtab[i].style.display = 'block';
}
</script>
But for this i am getting this error
Uncaught TypeError: Cannot read property 'style' of undefined
at showTable1234
Advance Thanks for the help.
javascript html
javascript html
edited Nov 22 '18 at 18:50
hev1
5,5783527
5,5783527
asked Nov 22 '18 at 13:34
AtulAtul
114
114
Please click edit then click the[<>]
snippet editor and create a Minimal, Complete, and Verifiable example
– mplungjan
Nov 22 '18 at 13:37
1
One error is that you submit the page so you will never see the result - change the button to a button<button type="button" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
or use AJAX
– mplungjan
Nov 22 '18 at 13:37
1
The other error is that this is ONE tabdocument.getElementsByClassName('container12')[0];
and cannot be looped over. Remove the[0]
– mplungjan
Nov 22 '18 at 13:39
Thanks for the answer
– Atul
Nov 22 '18 at 14:55
add a comment |
Please click edit then click the[<>]
snippet editor and create a Minimal, Complete, and Verifiable example
– mplungjan
Nov 22 '18 at 13:37
1
One error is that you submit the page so you will never see the result - change the button to a button<button type="button" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
or use AJAX
– mplungjan
Nov 22 '18 at 13:37
1
The other error is that this is ONE tabdocument.getElementsByClassName('container12')[0];
and cannot be looped over. Remove the[0]
– mplungjan
Nov 22 '18 at 13:39
Thanks for the answer
– Atul
Nov 22 '18 at 14:55
Please click edit then click the
[<>]
snippet editor and create a Minimal, Complete, and Verifiable example– mplungjan
Nov 22 '18 at 13:37
Please click edit then click the
[<>]
snippet editor and create a Minimal, Complete, and Verifiable example– mplungjan
Nov 22 '18 at 13:37
1
1
One error is that you submit the page so you will never see the result - change the button to a button
<button type="button" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
or use AJAX– mplungjan
Nov 22 '18 at 13:37
One error is that you submit the page so you will never see the result - change the button to a button
<button type="button" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
or use AJAX– mplungjan
Nov 22 '18 at 13:37
1
1
The other error is that this is ONE tab
document.getElementsByClassName('container12')[0];
and cannot be looped over. Remove the [0]
– mplungjan
Nov 22 '18 at 13:39
The other error is that this is ONE tab
document.getElementsByClassName('container12')[0];
and cannot be looped over. Remove the [0]
– mplungjan
Nov 22 '18 at 13:39
Thanks for the answer
– Atul
Nov 22 '18 at 14:55
Thanks for the answer
– Atul
Nov 22 '18 at 14:55
add a comment |
3 Answers
3
active
oldest
votes
Change this line:
<button type="submit" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
Into this and your issue should be resolved:
<button type="button" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
Thanks for the answer
– Atul
Nov 22 '18 at 14:55
Already posted this as a comment 15 min before this answer - why bother answering at all?
– mplungjan
Nov 22 '18 at 14:58
add a comment |
I suspect your submit button is in a form and that it is causing the form to submit. So the JavaScript works, but then the whole page gets refreshed due to the form submit.
To stop the form submit, you can add onSubmit=“return false”
to the form. You can also stop the submit by returning false from your button’s onClick, or use a button of type button
rather than submit
.
Thanks for the answer and the explanation
– Atul
Nov 22 '18 at 14:55
add a comment |
Add a return false at the end of the function and return the returned value of showTable1234
in the click handler of the button to prevent the default action (which is submitting the form and thus refreshing the page).
<button type="submit" class="btn btn-primary btn-block" onclick="return showTable1234();">Submit</button>
<script>
function showTable1234() {
var showtab = document.getElementsByClassName('container12');
for(var i =0;i !=showtab.length;i++)
showtab[i].style.display = 'block';
return false;
}
</script>
Thanks for the answer.
– Atul
Nov 22 '18 at 14:55
It is not recommended to have a submit button and return false. Either preventdefault the form submit or make the submit a button
– mplungjan
Nov 22 '18 at 14:59
@mplungjan Returning false does the same thing as event.preventDefault here. Also, this method is actually quite common.
– hev1
Nov 22 '18 at 18:41
It is still not recommended. Use the submit event and return false or preventDefault as you wish
– mplungjan
Nov 22 '18 at 19:02
@mplungjan Why so?
– hev1
Nov 23 '18 at 2:53
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432178%2fdiv-element-appearing-only-for-seconds%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Change this line:
<button type="submit" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
Into this and your issue should be resolved:
<button type="button" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
Thanks for the answer
– Atul
Nov 22 '18 at 14:55
Already posted this as a comment 15 min before this answer - why bother answering at all?
– mplungjan
Nov 22 '18 at 14:58
add a comment |
Change this line:
<button type="submit" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
Into this and your issue should be resolved:
<button type="button" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
Thanks for the answer
– Atul
Nov 22 '18 at 14:55
Already posted this as a comment 15 min before this answer - why bother answering at all?
– mplungjan
Nov 22 '18 at 14:58
add a comment |
Change this line:
<button type="submit" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
Into this and your issue should be resolved:
<button type="button" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
Change this line:
<button type="submit" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
Into this and your issue should be resolved:
<button type="button" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
<button type="submit" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
<button type="submit" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
<button type="button" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
<button type="button" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
answered Nov 22 '18 at 14:10
sbattousbattou
7311
7311
Thanks for the answer
– Atul
Nov 22 '18 at 14:55
Already posted this as a comment 15 min before this answer - why bother answering at all?
– mplungjan
Nov 22 '18 at 14:58
add a comment |
Thanks for the answer
– Atul
Nov 22 '18 at 14:55
Already posted this as a comment 15 min before this answer - why bother answering at all?
– mplungjan
Nov 22 '18 at 14:58
Thanks for the answer
– Atul
Nov 22 '18 at 14:55
Thanks for the answer
– Atul
Nov 22 '18 at 14:55
Already posted this as a comment 15 min before this answer - why bother answering at all?
– mplungjan
Nov 22 '18 at 14:58
Already posted this as a comment 15 min before this answer - why bother answering at all?
– mplungjan
Nov 22 '18 at 14:58
add a comment |
I suspect your submit button is in a form and that it is causing the form to submit. So the JavaScript works, but then the whole page gets refreshed due to the form submit.
To stop the form submit, you can add onSubmit=“return false”
to the form. You can also stop the submit by returning false from your button’s onClick, or use a button of type button
rather than submit
.
Thanks for the answer and the explanation
– Atul
Nov 22 '18 at 14:55
add a comment |
I suspect your submit button is in a form and that it is causing the form to submit. So the JavaScript works, but then the whole page gets refreshed due to the form submit.
To stop the form submit, you can add onSubmit=“return false”
to the form. You can also stop the submit by returning false from your button’s onClick, or use a button of type button
rather than submit
.
Thanks for the answer and the explanation
– Atul
Nov 22 '18 at 14:55
add a comment |
I suspect your submit button is in a form and that it is causing the form to submit. So the JavaScript works, but then the whole page gets refreshed due to the form submit.
To stop the form submit, you can add onSubmit=“return false”
to the form. You can also stop the submit by returning false from your button’s onClick, or use a button of type button
rather than submit
.
I suspect your submit button is in a form and that it is causing the form to submit. So the JavaScript works, but then the whole page gets refreshed due to the form submit.
To stop the form submit, you can add onSubmit=“return false”
to the form. You can also stop the submit by returning false from your button’s onClick, or use a button of type button
rather than submit
.
answered Nov 22 '18 at 13:41
Ryan CogswellRyan Cogswell
2,948416
2,948416
Thanks for the answer and the explanation
– Atul
Nov 22 '18 at 14:55
add a comment |
Thanks for the answer and the explanation
– Atul
Nov 22 '18 at 14:55
Thanks for the answer and the explanation
– Atul
Nov 22 '18 at 14:55
Thanks for the answer and the explanation
– Atul
Nov 22 '18 at 14:55
add a comment |
Add a return false at the end of the function and return the returned value of showTable1234
in the click handler of the button to prevent the default action (which is submitting the form and thus refreshing the page).
<button type="submit" class="btn btn-primary btn-block" onclick="return showTable1234();">Submit</button>
<script>
function showTable1234() {
var showtab = document.getElementsByClassName('container12');
for(var i =0;i !=showtab.length;i++)
showtab[i].style.display = 'block';
return false;
}
</script>
Thanks for the answer.
– Atul
Nov 22 '18 at 14:55
It is not recommended to have a submit button and return false. Either preventdefault the form submit or make the submit a button
– mplungjan
Nov 22 '18 at 14:59
@mplungjan Returning false does the same thing as event.preventDefault here. Also, this method is actually quite common.
– hev1
Nov 22 '18 at 18:41
It is still not recommended. Use the submit event and return false or preventDefault as you wish
– mplungjan
Nov 22 '18 at 19:02
@mplungjan Why so?
– hev1
Nov 23 '18 at 2:53
|
show 1 more comment
Add a return false at the end of the function and return the returned value of showTable1234
in the click handler of the button to prevent the default action (which is submitting the form and thus refreshing the page).
<button type="submit" class="btn btn-primary btn-block" onclick="return showTable1234();">Submit</button>
<script>
function showTable1234() {
var showtab = document.getElementsByClassName('container12');
for(var i =0;i !=showtab.length;i++)
showtab[i].style.display = 'block';
return false;
}
</script>
Thanks for the answer.
– Atul
Nov 22 '18 at 14:55
It is not recommended to have a submit button and return false. Either preventdefault the form submit or make the submit a button
– mplungjan
Nov 22 '18 at 14:59
@mplungjan Returning false does the same thing as event.preventDefault here. Also, this method is actually quite common.
– hev1
Nov 22 '18 at 18:41
It is still not recommended. Use the submit event and return false or preventDefault as you wish
– mplungjan
Nov 22 '18 at 19:02
@mplungjan Why so?
– hev1
Nov 23 '18 at 2:53
|
show 1 more comment
Add a return false at the end of the function and return the returned value of showTable1234
in the click handler of the button to prevent the default action (which is submitting the form and thus refreshing the page).
<button type="submit" class="btn btn-primary btn-block" onclick="return showTable1234();">Submit</button>
<script>
function showTable1234() {
var showtab = document.getElementsByClassName('container12');
for(var i =0;i !=showtab.length;i++)
showtab[i].style.display = 'block';
return false;
}
</script>
Add a return false at the end of the function and return the returned value of showTable1234
in the click handler of the button to prevent the default action (which is submitting the form and thus refreshing the page).
<button type="submit" class="btn btn-primary btn-block" onclick="return showTable1234();">Submit</button>
<script>
function showTable1234() {
var showtab = document.getElementsByClassName('container12');
for(var i =0;i !=showtab.length;i++)
showtab[i].style.display = 'block';
return false;
}
</script>
edited Nov 23 '18 at 2:54
answered Nov 22 '18 at 14:16
hev1hev1
5,5783527
5,5783527
Thanks for the answer.
– Atul
Nov 22 '18 at 14:55
It is not recommended to have a submit button and return false. Either preventdefault the form submit or make the submit a button
– mplungjan
Nov 22 '18 at 14:59
@mplungjan Returning false does the same thing as event.preventDefault here. Also, this method is actually quite common.
– hev1
Nov 22 '18 at 18:41
It is still not recommended. Use the submit event and return false or preventDefault as you wish
– mplungjan
Nov 22 '18 at 19:02
@mplungjan Why so?
– hev1
Nov 23 '18 at 2:53
|
show 1 more comment
Thanks for the answer.
– Atul
Nov 22 '18 at 14:55
It is not recommended to have a submit button and return false. Either preventdefault the form submit or make the submit a button
– mplungjan
Nov 22 '18 at 14:59
@mplungjan Returning false does the same thing as event.preventDefault here. Also, this method is actually quite common.
– hev1
Nov 22 '18 at 18:41
It is still not recommended. Use the submit event and return false or preventDefault as you wish
– mplungjan
Nov 22 '18 at 19:02
@mplungjan Why so?
– hev1
Nov 23 '18 at 2:53
Thanks for the answer.
– Atul
Nov 22 '18 at 14:55
Thanks for the answer.
– Atul
Nov 22 '18 at 14:55
It is not recommended to have a submit button and return false. Either preventdefault the form submit or make the submit a button
– mplungjan
Nov 22 '18 at 14:59
It is not recommended to have a submit button and return false. Either preventdefault the form submit or make the submit a button
– mplungjan
Nov 22 '18 at 14:59
@mplungjan Returning false does the same thing as event.preventDefault here. Also, this method is actually quite common.
– hev1
Nov 22 '18 at 18:41
@mplungjan Returning false does the same thing as event.preventDefault here. Also, this method is actually quite common.
– hev1
Nov 22 '18 at 18:41
It is still not recommended. Use the submit event and return false or preventDefault as you wish
– mplungjan
Nov 22 '18 at 19:02
It is still not recommended. Use the submit event and return false or preventDefault as you wish
– mplungjan
Nov 22 '18 at 19:02
@mplungjan Why so?
– hev1
Nov 23 '18 at 2:53
@mplungjan Why so?
– hev1
Nov 23 '18 at 2:53
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432178%2fdiv-element-appearing-only-for-seconds%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please click edit then click the
[<>]
snippet editor and create a Minimal, Complete, and Verifiable example– mplungjan
Nov 22 '18 at 13:37
1
One error is that you submit the page so you will never see the result - change the button to a button
<button type="button" class="btn btn-primary btn-block" onclick="showTable1234()">Submit</button>
or use AJAX– mplungjan
Nov 22 '18 at 13:37
1
The other error is that this is ONE tab
document.getElementsByClassName('container12')[0];
and cannot be looped over. Remove the[0]
– mplungjan
Nov 22 '18 at 13:39
Thanks for the answer
– Atul
Nov 22 '18 at 14:55