Is is possible to trigger the button type "submit"
on my confirm box using javascript? I wanted to trigger the submit button on my "OK"
option. I wanted to remove the <button type = "submit" class = "btn btn-primary">Okay!</button>
in my HTML form. I just wanted to trigger it under "OK"
option is this possible?
Javascript
<script>
function showApprove()
{
value = confirm("Approve this document?");
if (value == true)
{
// I WANTED TO TRIGGER HERE :)
}
else
{
}
}
function showReject()
{
value = confirm("Reject this document?");
if (value == true)
{
}
else
{
}
}
</script>
View
<input type="hidden" name = "id" value = "{{$list->id}}">
<div class = "radio">
<label><input type = "radio" onclick = "showApprove()" name = "status" id = "approve" value="1"> Approve</label>
</div>
<div class = "radio">
<label><input type = "radio" onclick = "showReject()" name = "status" id = "reject" value="0"> Reject</label>
</div>
<button type = "submit" class = "btn btn-primary">Okay!</button>
<input type = "hidden" name = "_token" value = "{{ Session::token() }}">
So my View looks like there's no button anymore in my HTML. I just wanted to trigger this in my confirm box.
Call the submit()
method on the form element to trigger a submit event
document.querySelector(".form-line").submit();
Call the submit function, add an id to your form id="myForm"
document.getElementById("myForm").submit();
You need to submit your form from javascript as :
function showApprove()
{
value = confirm("Approve this document?");
if (value == true)
{
document.getElementById("formId").submit();//please set id attribute to your form.
}
else
{
}
}
You can follow this link.
the .submit() answers are right they will submit the form, but if you need to click the buttonand not submit form for some reason using javascript (like , you need the button value or something) use the following code
HTML
<button type="submit" class="btn btn-primary" id="okbutton" >Okay!</button>
JAVASCRIPT
function showApprove()
{
value = confirm("Approve this document?");
if (value == true)
{
document.getElementById("okbutton").click();
}
else
{
}
}
Change :
<button type = "submit" class = "btn btn-primary">Okay!</button>
To :
<button type = "button" class = "btn btn-primary">Okay!</button>
Then :
<script>
function showApprove()
{
if(confirm("Approve this document?"))
{
document.getElementById("myForm").submit();
}
else
{
return false;
}
}
</script>