0

I have django form with name and email address as a form field in forms.py file. I am implementing that form in HTML Page with the help of {form.as_p}.

Now I have JavaScript inside that same HTML page with...

{% block javascript %}

<script>

</script>

{% endblock %}

Now How can I access both form variable inside this script tag. I want to validate data at client-side and than wants to send at server. Will getElementById() method works? and If so what I need to write for access my form variables or any other good alternative. Thanks

4
  • HTML produced by django or by any other means is just HTML Commented May 31, 2017 at 5:29
  • Thank you for such answers but I am not able to access that's why asked here. Better to help other instead of providing such answers. Commented May 31, 2017 at 5:31
  • I did not answer your question. I posted a comment. If you want usefull answers, please read stackoverflow.com/help/how-to-ask and stackoverflow.com/help/mcve Commented May 31, 2017 at 5:42
  • I already explained the issue briefly, I can't share code. If you can help then thank you in advance. Commented May 31, 2017 at 5:48

1 Answer 1

3

Yes, its possible. Django gives an id in format id_fieldname to each field you define in your Django form.

E.g. For name field, id in HTML will be id_name and For email_address field ,id in HTML will be id_email_address

Now you can simply use getElementById() in your JS with these ID for validation.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi, I already tried this, but It always showing me [object HTMLInputElement] instead of value.
For value of a text field use document.getElementById("id_name").value
Hi, It still blank means null value.
Have you wrote something in text box before calling above ?
Yes, I wrote but getting null value only.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.