I'm new to Javascript, and trying to figure out how to get the value of a variable in my HTML form. I have the following code kind of working, I put an alert in to test my logic and the else statement is working, but I can't seem to figure out how to get Javascript to submit my form if the value is set to on. I know my value is being interpretted correctly as I tested it with an alert as well. It just doesn't seem to want to submit the form. I have looked through SO and it seems that the Javascript submit syntax might have changed? Here is my Javascript function:
document.getElementById('ifon').onclick = function(){
var getval = Checkbox(this.form);
if (getval == 'on') {
document.getElementById("data").submit();
} else {
alert(getval);
}
}
And here is my HTML for the form in question:
<form id="data" name="form_name" action="{% url 'foo:bar' record.id %}">
<input type='hidden' name='status' value="Delete">
</form>
I'm new to Javascript so maybe I'm missing something. This is a checkbox and Javascript seems to know what the value is based on my testing. I just can't seem to get the form to submit. Thanks for the help in advance.
Just to simplify matters and my understanding of Javascript, I've played around a bit, and can confirm that I've got the basics down. Here's my form...
<form id="data" name="form_name" action="{% url 'foo:bar' record.id %}" method="POST">
<input type='hidden' name='status' value="Delete">
</form>
<script>
function submitform()
{
document.form_name.submit();
}
</script>
This code doesn't appear to work. No error messages either.
The basic Javascript principles are working, as when I change the script to something simple like...
<script>
function submitform()
{
alert("hi!");
}
</script>
I in fact see a pop up window that says hi! This is all predicated on a button that has the onclick outlined below. This all works fine.
<button type="button" onclick="submitform()" class="button9"><h3 class="txtalgn1">Link</h3></button></a>
The basics of Javascript in my install seem to work. I have updated my view to include as follows:
class BookUpdateView(LoginRequiredMixin,NeverCacheMixin,UpdateView):
model = Book
template_name = 'book/book_update.html'
form_class = book_Update
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(BookUpdateView, self).dispatch(request, *args, **kwargs)
def form_valid(self, form):
self.kwargs['status'] = form.cleaned_data['status']
return super(BookUpdateView, self).form_valid(form)
But the Javascript submit still doesn't appear to be working, or if it is it's not performing the Delete based on the status...