-2

I really don't know why these two functions - leave() & do() - don't run !

    function leave() 
    {
        var x = document.getElementById("x");
        if(x.value == "")
        {
            alert("please enter your name");
            x.focus();
        }
    }

    function do()
    {
        var y = document.getElementById("y");
        if (y.value = "enter your name here")
        {
            alert("enter your last name");
            y.focus();
            y.select();                
        }
    }

here is my code: http://jsfiddle.net/BsHa2

thanks in advance

Satpal
  • 132,252
  • 13
  • 159
  • 168
Omar Ahmed
  • 23
  • 1
  • 4
  • 2
    Post code **in the question itself**, don't just link. That's **why** the system prevented you from posting the jsfiddle link until you marked it up as code. Surely working around the system like that wasn't your best choice? – T.J. Crowder Feb 09 '14 at 11:38
  • [jsFiddle: no connection between html and js? Can't call simple function from button?](http://stackoverflow.com/questions/14499783/jsfiddle-no-connection-between-html-and-js-cant-call-simple-function-from-but) – Sirko Feb 09 '14 at 11:39
  • T.j. Crowder, sorry i didn't know that! but am i prevented from asking any question again ? – Omar Ahmed Feb 09 '14 at 11:46
  • @T.J.Crowder, am i prevented from asking any question again ? and why !? – Omar Ahmed Feb 09 '14 at 11:52
  • @OmarAhmed: Not that I know of, but I'm not a moderator (other than the way nearly all users are). Only (real) moderators can suspend or lock an account. – T.J. Crowder Feb 09 '14 at 12:02
  • @T.J.Crowder: he's referring to the question ban. – Qantas 94 Heavy Feb 09 '14 at 14:24
  • 1
    @OmarAhmed: take a look at [what to do if you are getting a "We are no longer accepting questions from this account" message](http://meta.stackexchange.com/a/86998/2074608). – Qantas 94 Heavy Feb 09 '14 at 14:29

4 Answers4

1

you have 3 problems:

1- which is in your jsfiddle options you have chosen to wrap all your code in the onLoad, so the functions are not in the global context, you can fix it as I have in the code below.

2- this line would set the value to the value of y input:

if (y.value = "enter your name here")

change it to

if (y.value == "enter your name here")

3- the other probelm is do is a reserved word, DO NOT USE reserved word, although it would do what you want in some browsers.

window.leave = function leave() 
{
    var x = document.getElementById("x");
    if(x.value == "")
    {
        alert("please enter your name");
        x.focus();
    }
}

window.check = function check()
{
    var y = document.getElementById("y");
    if (y.value = "enter your name here")
    {
        alert("enter your last name");
        y.focus();
        y.select();                
    }
}
Mehran Hatami
  • 12,723
  • 6
  • 28
  • 35
0

First do is a keyword so you can't use it as a method name - rename it to something like check

Second for inline event hadndlers the methods must be in global scope - Select body/head in the second dropdown in left panel in the fiddle

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

do is a reserved keyword. You can't use it as function name. Rename it to something else.

Secondly, inline event handlers must be defined in global scope. In your fiddle you have to select Wrap in head option

Third, = is assignment operator, to compare either use == and ===, error in line (y.value = "enter your name here")

Use

function do1()

DEMO

Satpal
  • 132,252
  • 13
  • 159
  • 168
0

do is a reserved keyword. You can't use it as function name.

Also, you have a mistake here:

if (y.value = "enter your name here")

you need to check for equality:

if (y.value === "enter your name here")

As an aside, you should really consider giving your variables meaningful names and using unobtrusive event handlers:

<form id="myForm">
    <label for="firstName">First Name:</label>
    <input type="text" name="input" id="firstName" size="20">
    <br/>
    <label for="lastName">Last Name:</label>
    <input type="text" id="lastName" size="20" value="enter your name here">
    <input type="button" id="check" value="Check!">
</form>

var firstName = document.getElementById("firstName"),
    lastName = document.getElementById("lastName"),
    checkButton = document.getElementById("check");

firstName.onblur = function(){
  if (this.value === ""){
    alert("please enter your name");
    this.focus();
  }  
}

check.onclick = function(e){
    e.preventDefault();
    if (lastName.value === "enter your name here") {
      alert("enter your last name");
      lastName.focus();
    }
}

fiddle

James Hibbard
  • 16,490
  • 14
  • 62
  • 74