0

I am trying to integrate a javascript form validation into a pet project of mine and am stumbling over this:

function validateForm() {

  if (this.firstname.value == "") {

    document.getElementById("testingline").innerHTML = '<div class="alert alert-danger alert-dismissable"><span>'+missing+'</span></div>';
    this.firstname.focus();

    return false;
  }

  return true;
}

If the line document.getElementById is omitted, the form is not submitted on click, while with the line the form is submitted.

What am I doing wrong?

The function is called through this:

 <form name="helperform" role="form" method="get" action="thanks.php"
    onsubmit="return validateForm(this)">

and the line should be added to this:

<div class="col-md-6 col-md-offset-3" id="testingline" name="testline"></div>

Thanks for helping!

Daniel

6
  • Please also return true at the end to allow the submission once you fixed the DOM/jQuery mix you have. BTW If you pass (this) in the validateForm(this) you do not need to name the form Commented Mar 9, 2014 at 16:29
  • @mplungjan: There's no need to return true. Good point about passing in this. Commented Mar 9, 2014 at 16:45
  • Yes there is. Some browsers will complain (function does not always return a value) and in any case it is good documentation to show that the idea is to allow the submission at the end Commented Mar 9, 2014 at 17:13
  • thx, I did not think about that. I added that. Doesn't change the problem though :-D Commented Mar 9, 2014 at 18:51
  • @mplungjan: No browser will complain that the function doesn't always return a value. Some lint tools might, but that's not the same thing at all. return true has exactly zero meaning in a DOM0 event handler, so including it is pointless code. Commented Mar 9, 2014 at 18:56

1 Answer 1

3

DOM elements don't have a function called html. You can set their contents using the property innerHTML, like this:

document.missing_placeholder.innerHTML = '<div class="alert alert-danger alert-dismissable"><span>'+Du hast was vergessen+'</span></div>';

But separately, note that you've ended the quotes too early, so your Du hast was vergessen is not in a string. So to fix that:

document.missing_placeholder.innerHTML = '<div class="alert alert-danger alert-dismissable"><span>Du hast was vergessen</span></div>';

Additionally, you haven't shown how you're defining the element you've called document.missing_placeholder above, but document.missing_placeholder is probably wrong. If you're defining it with an id, use document.getElementById("missing_placeholder") instead (although window.missing_placeholder may work).


The html function you see used frequently is from an add-on library, typically jQuery. But even if you were using jQuery, to use it, you'd have to get a jQuery wrapper for the element first:

$(/* document.missing_placeholder or whatever*/).html('<div class="alert alert-danger alert-dismissable"><span>Du hast was vergessen</span></div>');

But again, that's only if you're using jQuery.

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

6 Comments

Ok, it seems I mixed something there. I tried to just use javascript without anything else. I now changed the line to .getElementById("testingline").innerHTML but to no avail. I edited the Example above. The problem persists, that this line seems to make the statement true.
@ Kazschuri: Okay, so the first code example is what you need. BTW, I just noticed that you had a quotes issue as well, which I've noted and fixed in the above.
I noticed the quotes too :-D that is fixed and I use your line of document.testingline.innerHTML But the form still keeps submitting its content, as long as this line is there. Works fine if I substitute that line with an alert.
@Kazschuri: Did you read the rest of the answer? About how document.whatsit was probably wrong? And what to do about that?
document.getElementByID did not work. I edited that in the above example. I did not try window.testingline(was missing_placeholder) and did that now. that worked. Thx! What is the convention here how to mark the right answer for everyone?
|

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.