-1

I wrote these few lines of code and would like the text to change once the button is pressed but its not working. Could you please find out the problem?

var omari = "Omari Lamar";
function omari (){
    el = document.getElementById('slice');
    el.textContent = omari + "Is a computer Programmer!";
}
<html>
    <head>
        <title></title>
    </head>
    <body>
       <h1>Title Example</h1>
       <button onclick="omari();">Click me</button>
       <div id="slice">
           sample text
       </div>
       <script src="app.js"></script>
   </body>
</html>

3
  • 1
    What happens when you run your code now? Debug your code. Place a breakpoint on the el.textContent line, and examine the value of the variable omari. You should be able to figure it out from there. Or, if you used a smart editor, it would probably point out the problem with its syntax highlighting. Commented May 10, 2016 at 5:37
  • you have included "app.js" twice Commented May 10, 2016 at 5:39
  • 1
    When you ask a question you describe the expected behavior and the current behavior. You should report any errors you are getting. Please read How to Ask (especially about providing a specific title, the title you chose is useless). Commented May 10, 2016 at 5:41

3 Answers 3

2

Change the code to:

var omariName ="Omari Lamar";
function omari (){
    el = document.getElementById('slice');
    el.textContent = omariName + "Is a computer Programmer!";

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

2 Comments

You might explain that the variable name and the function name might be causing the conflict :-)
As jeff mentioned there is a conflict between your omari variable name and your function name.
-1

Here you go, Your function name and variable name were the same

var omary ="Omari Lamar";
var omari = function(){
	var el = document.getElementById('slice');
	el.innerHTML = omary + "Is a computer Programmer!";

};
<html>	
	<head>
		<title>

		</title>
		<script src="app.js"></script>
	</head>
	<body>
		<h1>Title Example</h1>

		<button onclick="omari();">Click me</button>
		<div id="slice">
		sample text
		</div>

		<script src="app.js"></script>
	</body>
</html>

Comments

-1

this is the problem: you have a variable named omari and a function name omari. so, when you try calling the function omari, javascript actually looks at the variable definition, and not the function. just give them different name.

try the code below.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>test</title>
</head>

<body>
<script>

    var _omari ="Omari Lamar";
    function omari(){
        el = document.getElementById('slice');
        el.innerHTML = _omari + " Is a computer Programmer!";

    }
</script>

            <h1>Title Example</h1>

            <button onclick="omari();">Click me</button>
            <div id="slice">
            sample text
            </div>

</body>
</html>

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.