views:

78

answers:

4

In my HTML file, I have a JavaScript function named "CheckCaptcha". I am using following code line to execute that function when an image is clicked. It is not working. After hours of tweaking and modifying the code, it is not simply reaching to the location where the JavaScript function lies.

I am using this code line:

<input type="image" src="images/join.gif" name="SubmitStudent" onclick="CheckCaptcha();" width="98" height="31"/>

The JavaScript section is as below:

<script type="text/javascript">
function CheckCaptcha()
{
    aler("-");
    var CaptchaWord;
    CaptchaWord = document.getElementById(StudentCaptchaImage);
    alert(CaptchaWord);
    if (CaptchaWord.length <= 4)
    {
     alert("Please enter words as seen in the image.");
     return false;
    } 
}   
</Script>

Actually, I am a new user, and StackOverflow is avoiding me to post the entire HTML section.

+1  A: 

Have checked for any errors in JavaScript which may not be related to this particular function? Use Firebug to find any errors I tried following code and its working

<script>
    function CheckCaptcha(){
     alert("test");
    }
</script>
<input type="image" src="images/join.gif" name="SubmitStudent" onclick="CheckCaptcha();" width="98" height="31"/>
Xinus
+1 one for advice on debugging!
lexu
A: 

Have you tried returning false to cancel the event bubble?

onclick="CheckCaptcha(); return false"
The Who
+4  A: 

Try this:

function CheckCaptcha()
{
    alert("-");
    var CaptchaWord = document.getElementById('StudentCaptchaImage');
    alert(CaptchaWord);
    if (CaptchaWord.value.length <= 4)
    {
        alert("Please enter words as seen in the image.");
        return false;
    }   
}
  • the .value attribute contains the value, the DOM element does not have a length property.
  • the gEBI method takes a string, not an identifier as you had it.. unless it was a variable.
  • your example had a typo in the first alert, I'm guessing you recently added it and it was not the initial problem.

If you're not sure of anything you should start alerting as many things as possible..

alert( typeof CaptchaWord.length )
meder
I was using double quotes within getElementByID, whereas a single quote is needed. Thanks.
RPK
+2  A: 

Make sure getElementById takes a string:

CaptchaWord = document.getElementById("StudentCaptchaImage");
Kobi
+1 ... I think thats the problem
Xinus
your DOM element doesn't have a `length` property - try mine if this doesn't fully do it.
meder