2

I am writing my first code in JavaScript.

I want to do this :-

  • Load an image
  • Ask for user name
  • after writing the name change the image
  • and show user's name in alert

My code goes like this:

<!DOCTYPE html>
<html>
<head>
<title>iRock - First Project</title>
<script type = "text/javascript">

function touchRock(){

    var userName = prompt ("what is your name?","Enter your name here.");

    if(userName){
        alert("It is good to meet you, " "+userName+ " ".");
        document.getElementById("firstImg").src="1.jpg";
    }
}

</script>
</head>

<body onload = "alert('hello, I am your pet.');">
        <div style = "margin-top:100px; text-align:centre">
                 <img id="firstImg" src="http://www.smiley-faces.org/wallpaper/smiley-face-wallpaper-widescreen-001.jpg" alt="iRock" style="cursor:pointer" onclick="touchRock()" />
        </div>
    </body>
</html>

Can anyone please tell me what is wrong in this? The event is not getting called after touching the image.

4
  • 2
    First thing you should do is learn to use the Javascript console in your browser. It will point out when you have syntax errors in your code. Commented May 6, 2013 at 19:37
  • jsfiddle.net/DfSHS -- error in your alert statement, fiddle is the fix. Commented May 6, 2013 at 19:38
  • 1
    @user814628 If you're going to answer the question, post it in an answer, not a comment. Commented May 6, 2013 at 19:39
  • Head first Javascript? :D Commented Jan 2, 2015 at 21:40

3 Answers 3

7

Your string concatination is wrong inside the function

alert("It is good to meet you, " "+userName+ " "."); you must be getting error in the console.

Check this

function touchRock(){

    var userName = prompt ("what is your name?","Enter your name here.");

    if(userName){
        alert("It is good to meet you, " + userName +  ".");
        document.getElementById("firstImg").src="1.jpg";
    }
}

if you are using Chrome. press F12 --> you will get developer tool bar enabled, in that there will be console where you can see any errors, debug javascript inspect elements etc.. SImilarly you have Firebug for FireFox and DevToolbar for Int Explorer.

Script Debugger in Chrome - Ref

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man , So fast , It worked like a charm . BTW , I can only accept answer after 11 Mins :(
For Chrome/mac SHortcut keys check this--> developers.google.com/chrome-developer-tools/docs/shortcuts
1

You are not preforming string concatenation correctly in your alert.

Change this:

alert("It is good to meet you, " "+userName+ " ".");

To this:

alert("It is good to meet you, " + userName + ".");

To concatenate two strings together the + operator must sit outside of the strings.

var newString = "I am " + "a concatenated string";

Comments

1

i think u can use alert( "It is good to meet you, "+ userName + "." ) ;

instead of alert("It is good to meet you, " "+userName+ " ".");

As per my knowledge, in javascript we usually use "+" symbol for concatenation .

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.