-2

here is my code:

<form action="eventssearch.php" method="post" onsubmit="return checkDate();">
    <p>
        <label for="date1">Date in the format DD/MM/YYYY:</label><br />
        <input type="text" id="date" name="date11" />
        <input type="submit" value="Check " />
        <br />(example 26/04/1975) <span class="error"> </span>
    </p>
</form>

I wrote the following code which didnt work:

var dateField=document.getElementById('error');
dateField.value = "hi Mom"

Is there some kind of parent-child relationship here which I am not seeing ? If so, how can I spot it ?

Preview
  • 35,317
  • 10
  • 92
  • 112
user3551969
  • 19
  • 1
  • 4

3 Answers3

1

First issue, you are calling the function getElementById('error'), when the span element has a class 'error', not an id. Hence it will never find it. You should change the class="error" to id="error". Then you can say dateFiled.innerHtml = "Hi Mom" instead of .value

antoniovassell
  • 1,012
  • 14
  • 24
0
<span class="error"></span>

<script>
    var dateField = document.getElementsByClassName('error');
    dateField[0].innerHTML = 'hi Mom';

    //or you could use
    var dateField2 = document.getElementByClassName('error');
    dateField2[0].textContent = 'hi Mom'; // this is using dom nodes
</script>

view fiddle http://jsfiddle.net/h_awk/tFrx7/

Get the element by class name if it is span class=error

Hawk
  • 788
  • 1
  • 6
  • 18
0

Firstly you should use right method to get class instance but you are using getElementById() for getting class instance. Use getElementsByClassName() or querySelector() method and second, I think you should use innerHTML if you are using javascript or if you are using jquery then use text() or html() method.

Solution:

var err = document.getElementsByClassName('error')[0];
err.innerHTML = "say hi!";
Alexey Malev
  • 6,408
  • 4
  • 34
  • 52
Pankaj Bisht
  • 986
  • 1
  • 8
  • 27