Author Topic: Simple Javascript Help  (Read 699 times)

0 Members and 1 Guest are viewing this topic.

Offline Dijkstra

  • Serf
  • *
  • Posts: 44
  • Cookies: 6
    • View Profile
Simple Javascript Help
« on: February 07, 2015, 02:47:28 pm »
I am not sure what I am doing wrong. I have a simple HTML file that points to a javascript function.

HTML
--------------------
Code: [Select]
<!DOCTYPE html>
<html>
<head>
<title>Javascript Test</title>
<script type="text/javascript" src="Test.js"></script>
</head>


<body>
<input type="text" name="txtFirstName" id="txtFirstName" placeholder="First Name" />
<br />
<br />
<input type="text" name="txtLastName" id="txtLastName" placeholder="Last Name" />
<br />
<br />
<button onclick="CombineName();">Click Me</button>
</body>
</html>


JS file is: Test.js
--------------------------
Code: [Select]
function CombineName()
{

var FirstName = document.getElementByld("txtFirstName").innerText;
var LastName = document.getElementByld('txtLastName')/innerText;
CombineName5 = txtFirstName + txtLastName;
alert(CombineName5);

}

------------------------------------

I cannot get my browser to show me an alert. Any thoughts as to why?

I took the liberty of adding /code/ tags to your post - Phage
« Last Edit: February 07, 2015, 03:32:35 pm by Phage »

Offline Rytiou

  • Noob Zombie
  • VIP
  • Knight
  • *
  • Posts: 199
  • Cookies: 45
  • EZ's Noob
    • View Profile
Re: Simple Javascript Help
« Reply #1 on: February 07, 2015, 04:02:10 pm »
Figured it out. First off, your getElementById's had an L in it instead of an I "getElementByld".When I figured that out and got the alert to appear there wasn't any text in your variables showing up so I put .value on the end of the variables that you were making which got it to show up. Hopefully this helped and this was explained well enough as too why it wasn't working. Here's the code below.

HTML

Code: [Select]
<!DOCTYPE html>
<html>
<head>
   <title>Javascript Test</title>
   <script src="Test.js"></script>
</head>


<body>
   <input type="text" name="txtFirstName" id="txtFirstName" placeholder="First Name" />
   <br />
   <br />
   <input type="text" name="txtLastName" id="txtLastName" placeholder="Last Name" />
   <br />
   <br />
   <button onclick="CombineName();">Click Me</button>

</body>
</html>



JavaScript

Code: [Select]
function CombineName()
{
var FirstName = document.getElementById("txtFirstName").value;
var LastName = document.getElementById("txtLastName").value;
CombineName5 = FirstName + " " + LastName;
alert("Hello, " + CombineName5);
}

« Last Edit: February 07, 2015, 04:04:20 pm by Rytiou »
Quote from: Evilzone IRC
<EZBot> life, you're so lame we decided to change your nick to Rytiou's bitch.
<EZBot> Rytiou is lord of the fags and will suck dicks for shells.