5

First of all, is it even possible to write if/else statements directly in html with onclick attribute? And if so, why is my code not working?

So this is a button. The "Calc.Input.value" refers to a text input and I want to display an error message if that field is 0 or blank. In all other cases I want some other things to happen as you can see. This is not relevant though, as everything after the "else" worked fine before.

<INPUT TYPE="button" NAME="drop" id="dropbutton" style="background-color:#D1E8D5;" VALUE="
Släpp ankare " OnClick="if ("Calc.Input.value == ''" || "Calc.Input.value == '0'")
{window.alert("Please enter a number");} else 
{document.getElementById('dropbutton').value=' Justera 
längd ';document.getElementById('length').innerHTML =
Calc.Input.value;document.getElementById('dropbutton').style.cssText = 'background-
color:#FFF6B3;';}">
6
  • 3
    Oy...Even if it would work (which it doesn't), that doesn't look too pretty. :/ Why don't you just have a function call in the onclick and perform the conditional expressions within the function? Commented Mar 12, 2013 at 19:37
  • The Obtrusiveness burns my eyes!!! This is NOT how good JavaScript should be written Commented Mar 12, 2013 at 19:39
  • 4
    You have broken the internet Commented Mar 12, 2013 at 19:40
  • The if ("Calc.Input.value == ''" || "Calc.Input.value == '0'") line is strange … Commented Mar 12, 2013 at 19:41
  • 1
    Needs more document.write(). Commented Mar 12, 2013 at 19:47

5 Answers 5

23

Just DON'T put it in the onclick attribute.

Use

<INPUT TYPE="button" NAME="drop" id="dropbutton" style="background-color:#D1E8D5;" VALUE="Släpp ankare ">
<script>
document.getElementById('dropbutton').onclick = function() {
    if (Calc.Input.value == '' || Calc.Input.value == '0') {
         window.alert("Please enter a number");
    } else {
        document.getElementById('dropbutton').value=' Justera längd ';
        document.getElementById('length').innerHTML = Calc.Input.value;
        document.getElementById('dropbutton').style.backgroundColor = '#FFF6B3';
    }
    return false;
}
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

The extra quotes in the if statement are obviously not supposed to be there.
I'd go so far as to suggest leaving the reurn false inside the function scope as well...
I will retreat to my chambers for some wallowing in shame...but thank you, this works like a charm :-)
4

The reason your code is not working is because you need to escape your " quotation marks, otherwise it will be interpreted as the HTML attribute symbol.

And I agree with the others, it is a bad practice to write your JavaScript inline inside your HTML.

Comments

2

sweetamylase's reply is best but you may also consider:

If your onclick= assignment is enclosed in double quotes (onclick="...") then try using only single quote characters inside of those double quotes (or vice versa).

This can make things easier when you need to make assignments to the onclick event when inserting elements dynamically and having separate script functions isn't really a good option.

Example:

<button onclick="alert('Do this'+' and that')">Press This</button>

Comments

2

Hmm but how about for something like this:

<a href="#" onclick="if (a_function_to_get_language() == 'en') {
    alert('Some message');
}
else {
    alert('Some message in a different language');
}">Action</a>

Comments

0

  • Alert on nav page
  •            <script>
                  function myFunction() {
                alert("You Are Not Signed In!");
                       }
                     </script>
    

    Comments

    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.