Recognize form submission action by anchor
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
■ BACKGROUND:
I have an html like below, which has 1 button, 2 anchors, and a input to hold value wrapped in a form.
The goal i want to achieve is:
- be able to submit the form no matter from clicking both button and Anchor.
- using post.
- be able to recognized which element causes a submit.(e.g. button id or anchor id)
■ ISSUE:
The problem I am facing is that "since anchors are not focusable, the $(document.activeElement) will not recognize it if submits are triggerred
by clicking anchors" (learned from google result...).
■ QUESTION:
So, are there any easy(less changes to my current code) alternative ways to achieve my goal or simply how to recognize submission from anchors so that I can possibly capture its information just like what I
can do for button submission in this case?
thanks
<form id="detailsForm" method="post" class="ui form" enctype="application/x-www-form-urlencoded" action="someUrl">
...
<button id="prodDelButton" form="detailsForm">
delete
</button>
...
<input type="text" id="pickupSearchCriteria.pageNumber" name="pickupSearchCriteria.pageNumber" value="1" />
<a class="item paginationAnchor" id="1" onclick="document.getElementById('detailsForm').submit();" shape="rect">page1</a>
<a class="item paginationAnchor" id="2" onclick="document.getElementById('detailsForm').submit();" shape="rect">page2</a>
...
...
</form>
...
<script id="shared_index_before">
$('#detailsForm').submit(function () {
var $btn = $(document.activeElement);
if ($btn.prop('id') == 'stgDelButton') {
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
else if ($btn.prop('id') == 'prodDelButton') {
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
else if($btn.hasClass( "paginationAnchor" )){
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
});
</script>
javascript jquery html anchor
add a comment |
■ BACKGROUND:
I have an html like below, which has 1 button, 2 anchors, and a input to hold value wrapped in a form.
The goal i want to achieve is:
- be able to submit the form no matter from clicking both button and Anchor.
- using post.
- be able to recognized which element causes a submit.(e.g. button id or anchor id)
■ ISSUE:
The problem I am facing is that "since anchors are not focusable, the $(document.activeElement) will not recognize it if submits are triggerred
by clicking anchors" (learned from google result...).
■ QUESTION:
So, are there any easy(less changes to my current code) alternative ways to achieve my goal or simply how to recognize submission from anchors so that I can possibly capture its information just like what I
can do for button submission in this case?
thanks
<form id="detailsForm" method="post" class="ui form" enctype="application/x-www-form-urlencoded" action="someUrl">
...
<button id="prodDelButton" form="detailsForm">
delete
</button>
...
<input type="text" id="pickupSearchCriteria.pageNumber" name="pickupSearchCriteria.pageNumber" value="1" />
<a class="item paginationAnchor" id="1" onclick="document.getElementById('detailsForm').submit();" shape="rect">page1</a>
<a class="item paginationAnchor" id="2" onclick="document.getElementById('detailsForm').submit();" shape="rect">page2</a>
...
...
</form>
...
<script id="shared_index_before">
$('#detailsForm').submit(function () {
var $btn = $(document.activeElement);
if ($btn.prop('id') == 'stgDelButton') {
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
else if ($btn.prop('id') == 'prodDelButton') {
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
else if($btn.hasClass( "paginationAnchor" )){
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
});
</script>
javascript jquery html anchor
Don't rely on the active element, and don't call the submission directly. Create an event listener for the clicks, extract the needed information from the event object, and then call a function that will submit the actual form. You might wanna do a preventDefault() on the form submission, in order to handle this whole thing manually. Hope i understood your problem correctly.
– sheff2k1
Nov 22 '18 at 11:25
@sheff2k1 thanks for your quick comment. I don't have much experience with javascript, but I think what you described makes sense, I'd really appreciate if your can share a code sample.
– zaozaoer
Nov 22 '18 at 13:39
add a comment |
■ BACKGROUND:
I have an html like below, which has 1 button, 2 anchors, and a input to hold value wrapped in a form.
The goal i want to achieve is:
- be able to submit the form no matter from clicking both button and Anchor.
- using post.
- be able to recognized which element causes a submit.(e.g. button id or anchor id)
■ ISSUE:
The problem I am facing is that "since anchors are not focusable, the $(document.activeElement) will not recognize it if submits are triggerred
by clicking anchors" (learned from google result...).
■ QUESTION:
So, are there any easy(less changes to my current code) alternative ways to achieve my goal or simply how to recognize submission from anchors so that I can possibly capture its information just like what I
can do for button submission in this case?
thanks
<form id="detailsForm" method="post" class="ui form" enctype="application/x-www-form-urlencoded" action="someUrl">
...
<button id="prodDelButton" form="detailsForm">
delete
</button>
...
<input type="text" id="pickupSearchCriteria.pageNumber" name="pickupSearchCriteria.pageNumber" value="1" />
<a class="item paginationAnchor" id="1" onclick="document.getElementById('detailsForm').submit();" shape="rect">page1</a>
<a class="item paginationAnchor" id="2" onclick="document.getElementById('detailsForm').submit();" shape="rect">page2</a>
...
...
</form>
...
<script id="shared_index_before">
$('#detailsForm').submit(function () {
var $btn = $(document.activeElement);
if ($btn.prop('id') == 'stgDelButton') {
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
else if ($btn.prop('id') == 'prodDelButton') {
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
else if($btn.hasClass( "paginationAnchor" )){
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
});
</script>
javascript jquery html anchor
■ BACKGROUND:
I have an html like below, which has 1 button, 2 anchors, and a input to hold value wrapped in a form.
The goal i want to achieve is:
- be able to submit the form no matter from clicking both button and Anchor.
- using post.
- be able to recognized which element causes a submit.(e.g. button id or anchor id)
■ ISSUE:
The problem I am facing is that "since anchors are not focusable, the $(document.activeElement) will not recognize it if submits are triggerred
by clicking anchors" (learned from google result...).
■ QUESTION:
So, are there any easy(less changes to my current code) alternative ways to achieve my goal or simply how to recognize submission from anchors so that I can possibly capture its information just like what I
can do for button submission in this case?
thanks
<form id="detailsForm" method="post" class="ui form" enctype="application/x-www-form-urlencoded" action="someUrl">
...
<button id="prodDelButton" form="detailsForm">
delete
</button>
...
<input type="text" id="pickupSearchCriteria.pageNumber" name="pickupSearchCriteria.pageNumber" value="1" />
<a class="item paginationAnchor" id="1" onclick="document.getElementById('detailsForm').submit();" shape="rect">page1</a>
<a class="item paginationAnchor" id="2" onclick="document.getElementById('detailsForm').submit();" shape="rect">page2</a>
...
...
</form>
...
<script id="shared_index_before">
$('#detailsForm').submit(function () {
var $btn = $(document.activeElement);
if ($btn.prop('id') == 'stgDelButton') {
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
else if ($btn.prop('id') == 'prodDelButton') {
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
else if($btn.hasClass( "paginationAnchor" )){
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
});
</script>
<form id="detailsForm" method="post" class="ui form" enctype="application/x-www-form-urlencoded" action="someUrl">
...
<button id="prodDelButton" form="detailsForm">
delete
</button>
...
<input type="text" id="pickupSearchCriteria.pageNumber" name="pickupSearchCriteria.pageNumber" value="1" />
<a class="item paginationAnchor" id="1" onclick="document.getElementById('detailsForm').submit();" shape="rect">page1</a>
<a class="item paginationAnchor" id="2" onclick="document.getElementById('detailsForm').submit();" shape="rect">page2</a>
...
...
</form>
...
<script id="shared_index_before">
$('#detailsForm').submit(function () {
var $btn = $(document.activeElement);
if ($btn.prop('id') == 'stgDelButton') {
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
else if ($btn.prop('id') == 'prodDelButton') {
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
else if($btn.hasClass( "paginationAnchor" )){
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
});
</script>
<form id="detailsForm" method="post" class="ui form" enctype="application/x-www-form-urlencoded" action="someUrl">
...
<button id="prodDelButton" form="detailsForm">
delete
</button>
...
<input type="text" id="pickupSearchCriteria.pageNumber" name="pickupSearchCriteria.pageNumber" value="1" />
<a class="item paginationAnchor" id="1" onclick="document.getElementById('detailsForm').submit();" shape="rect">page1</a>
<a class="item paginationAnchor" id="2" onclick="document.getElementById('detailsForm').submit();" shape="rect">page2</a>
...
...
</form>
...
<script id="shared_index_before">
$('#detailsForm').submit(function () {
var $btn = $(document.activeElement);
if ($btn.prop('id') == 'stgDelButton') {
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
else if ($btn.prop('id') == 'prodDelButton') {
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
else if($btn.hasClass( "paginationAnchor" )){
$("#pickupSearchCriteria.pageNumber").val($btn.prop('id'));
}
});
</script>
javascript jquery html anchor
javascript jquery html anchor
asked Nov 22 '18 at 11:08
zaozaoerzaozaoer
74110
74110
Don't rely on the active element, and don't call the submission directly. Create an event listener for the clicks, extract the needed information from the event object, and then call a function that will submit the actual form. You might wanna do a preventDefault() on the form submission, in order to handle this whole thing manually. Hope i understood your problem correctly.
– sheff2k1
Nov 22 '18 at 11:25
@sheff2k1 thanks for your quick comment. I don't have much experience with javascript, but I think what you described makes sense, I'd really appreciate if your can share a code sample.
– zaozaoer
Nov 22 '18 at 13:39
add a comment |
Don't rely on the active element, and don't call the submission directly. Create an event listener for the clicks, extract the needed information from the event object, and then call a function that will submit the actual form. You might wanna do a preventDefault() on the form submission, in order to handle this whole thing manually. Hope i understood your problem correctly.
– sheff2k1
Nov 22 '18 at 11:25
@sheff2k1 thanks for your quick comment. I don't have much experience with javascript, but I think what you described makes sense, I'd really appreciate if your can share a code sample.
– zaozaoer
Nov 22 '18 at 13:39
Don't rely on the active element, and don't call the submission directly. Create an event listener for the clicks, extract the needed information from the event object, and then call a function that will submit the actual form. You might wanna do a preventDefault() on the form submission, in order to handle this whole thing manually. Hope i understood your problem correctly.
– sheff2k1
Nov 22 '18 at 11:25
Don't rely on the active element, and don't call the submission directly. Create an event listener for the clicks, extract the needed information from the event object, and then call a function that will submit the actual form. You might wanna do a preventDefault() on the form submission, in order to handle this whole thing manually. Hope i understood your problem correctly.
– sheff2k1
Nov 22 '18 at 11:25
@sheff2k1 thanks for your quick comment. I don't have much experience with javascript, but I think what you described makes sense, I'd really appreciate if your can share a code sample.
– zaozaoer
Nov 22 '18 at 13:39
@sheff2k1 thanks for your quick comment. I don't have much experience with javascript, but I think what you described makes sense, I'd really appreciate if your can share a code sample.
– zaozaoer
Nov 22 '18 at 13:39
add a comment |
1 Answer
1
active
oldest
votes
<form id="detailsForm" method="post" class="ui form" enctype="application/x-www-form-
urlencoded" action="someUrl">
<button class="submitableElement" id="prodDelButton" >
delete
</button>
<input type="text" id="pickupSearchCriteria.pageNumber"name="pickupSearchCriteria.pageNumber" value="1" />
<a class="item paginationAnchor submitableElement" id="1" shape="rect">page1</a>
<a class="item paginationAnchor submitableElement" id="2" shape="rect">page2</a>
</form>
The js:
$('.submitableElement').click(function(event){
const id = event.target.id;
doSomeThingWithId(id)//You can use the item id to know what was clicked.
$('#detailsForm').submit()
})
function doSomeThingWithId(id){
alert(`Element with id ${id} was used to submit the form...`);
//Do something here with the data...
}
I think that this is more or less what you need, but i would recommend you to reconsider if what you're doing there is actually needed.
My fiddle: https://jsfiddle.net/ku4rgqne/
Thanks very much! yours works.
– zaozaoer
Nov 23 '18 at 8:14
add a 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%2f53429626%2frecognize-form-submission-action-by-anchor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
<form id="detailsForm" method="post" class="ui form" enctype="application/x-www-form-
urlencoded" action="someUrl">
<button class="submitableElement" id="prodDelButton" >
delete
</button>
<input type="text" id="pickupSearchCriteria.pageNumber"name="pickupSearchCriteria.pageNumber" value="1" />
<a class="item paginationAnchor submitableElement" id="1" shape="rect">page1</a>
<a class="item paginationAnchor submitableElement" id="2" shape="rect">page2</a>
</form>
The js:
$('.submitableElement').click(function(event){
const id = event.target.id;
doSomeThingWithId(id)//You can use the item id to know what was clicked.
$('#detailsForm').submit()
})
function doSomeThingWithId(id){
alert(`Element with id ${id} was used to submit the form...`);
//Do something here with the data...
}
I think that this is more or less what you need, but i would recommend you to reconsider if what you're doing there is actually needed.
My fiddle: https://jsfiddle.net/ku4rgqne/
Thanks very much! yours works.
– zaozaoer
Nov 23 '18 at 8:14
add a comment |
<form id="detailsForm" method="post" class="ui form" enctype="application/x-www-form-
urlencoded" action="someUrl">
<button class="submitableElement" id="prodDelButton" >
delete
</button>
<input type="text" id="pickupSearchCriteria.pageNumber"name="pickupSearchCriteria.pageNumber" value="1" />
<a class="item paginationAnchor submitableElement" id="1" shape="rect">page1</a>
<a class="item paginationAnchor submitableElement" id="2" shape="rect">page2</a>
</form>
The js:
$('.submitableElement').click(function(event){
const id = event.target.id;
doSomeThingWithId(id)//You can use the item id to know what was clicked.
$('#detailsForm').submit()
})
function doSomeThingWithId(id){
alert(`Element with id ${id} was used to submit the form...`);
//Do something here with the data...
}
I think that this is more or less what you need, but i would recommend you to reconsider if what you're doing there is actually needed.
My fiddle: https://jsfiddle.net/ku4rgqne/
Thanks very much! yours works.
– zaozaoer
Nov 23 '18 at 8:14
add a comment |
<form id="detailsForm" method="post" class="ui form" enctype="application/x-www-form-
urlencoded" action="someUrl">
<button class="submitableElement" id="prodDelButton" >
delete
</button>
<input type="text" id="pickupSearchCriteria.pageNumber"name="pickupSearchCriteria.pageNumber" value="1" />
<a class="item paginationAnchor submitableElement" id="1" shape="rect">page1</a>
<a class="item paginationAnchor submitableElement" id="2" shape="rect">page2</a>
</form>
The js:
$('.submitableElement').click(function(event){
const id = event.target.id;
doSomeThingWithId(id)//You can use the item id to know what was clicked.
$('#detailsForm').submit()
})
function doSomeThingWithId(id){
alert(`Element with id ${id} was used to submit the form...`);
//Do something here with the data...
}
I think that this is more or less what you need, but i would recommend you to reconsider if what you're doing there is actually needed.
My fiddle: https://jsfiddle.net/ku4rgqne/
<form id="detailsForm" method="post" class="ui form" enctype="application/x-www-form-
urlencoded" action="someUrl">
<button class="submitableElement" id="prodDelButton" >
delete
</button>
<input type="text" id="pickupSearchCriteria.pageNumber"name="pickupSearchCriteria.pageNumber" value="1" />
<a class="item paginationAnchor submitableElement" id="1" shape="rect">page1</a>
<a class="item paginationAnchor submitableElement" id="2" shape="rect">page2</a>
</form>
The js:
$('.submitableElement').click(function(event){
const id = event.target.id;
doSomeThingWithId(id)//You can use the item id to know what was clicked.
$('#detailsForm').submit()
})
function doSomeThingWithId(id){
alert(`Element with id ${id} was used to submit the form...`);
//Do something here with the data...
}
I think that this is more or less what you need, but i would recommend you to reconsider if what you're doing there is actually needed.
My fiddle: https://jsfiddle.net/ku4rgqne/
answered Nov 22 '18 at 18:45
sheff2k1sheff2k1
466417
466417
Thanks very much! yours works.
– zaozaoer
Nov 23 '18 at 8:14
add a comment |
Thanks very much! yours works.
– zaozaoer
Nov 23 '18 at 8:14
Thanks very much! yours works.
– zaozaoer
Nov 23 '18 at 8:14
Thanks very much! yours works.
– zaozaoer
Nov 23 '18 at 8:14
add a 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%2f53429626%2frecognize-form-submission-action-by-anchor%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
Don't rely on the active element, and don't call the submission directly. Create an event listener for the clicks, extract the needed information from the event object, and then call a function that will submit the actual form. You might wanna do a preventDefault() on the form submission, in order to handle this whole thing manually. Hope i understood your problem correctly.
– sheff2k1
Nov 22 '18 at 11:25
@sheff2k1 thanks for your quick comment. I don't have much experience with javascript, but I think what you described makes sense, I'd really appreciate if your can share a code sample.
– zaozaoer
Nov 22 '18 at 13:39