0

Hello Guys I have a textbox which if the value inside the textbox is zero. It will pop up alert. but no alert is happening.

<input type="text" id="numbers" value= "0" >



                  <th style="width:5%;"><h4><input id="submitbutton" onclick="clicked();" class="btn btn-success" type="submit" name="<?php if($final==true){ echo "viewsummary" ;}else{ echo "next";} ?>" value="<?php if($final==true){ echo "View Summary" ;}else{ echo "Submit";} ?>" class="subbtn"/>

                    <script language="javascript">

                    document.getElementById('submitbutton')
                    function clicked(){
                        if (document.getElementById('numbers') == 0) {
                        window.alert("Please Play the Audio First");
                    }

                    }                        
                    </script>

I dont know whats the error If I look at the code for me its just fine? whats wrong? Thank you guys :")

1
  • You might be encountering script error, look at this line which is not terminated properly or not written correctly. document.getElementById('submitbutton') Commented Apr 17, 2015 at 15:30

2 Answers 2

1

Use this:

function clicked()
{
   if (document.getElementById('numbers') && document.getElementById('numbers').value == 0)
   {
     window.alert("Please Play the Audio First");
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Nathaniel, if it works for you, mark it as the accepted answer so someone else facing similar problem can benefit from it.
Yes sir u just wait 5 min
0

You need to check the value, not the element:

document.getElementById('numbers').value == "0"

Also, value will be a string and not a number. Full code:

//document.getElementById('submitbutton')
//^^ this does nothing

function clicked() {
    if (document.getElementById('numbers').value === "0") {
        window.alert("Please Play the Audio First");
    }

}

Don't forget, inside a form this may trigger the form action. "Please Play the Audio First" indicates that you don't want the form to submit if that if statement is hit, so you should return a value to stop that happening:

function clicked() {
    if (document.getElementById('numbers').value === "0") {
        window.alert("Please Play the Audio First");
    }
    return false;
}

HTML:

<input id="submitbutton" onclick="return clicked();" .... />

2 Comments

Its still not working sir huhuhu no alert happens :(
Check here: jsfiddle.net/sdzg2hr0 - You'd need to see why yours differs from this working version

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.