Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables Combining Strings

concatenate not working . value variables = document.getElementById("new-var") - require expert help

Hey people I have to concatenate a list of value variables as in following example <script> var nameInput = document.getElementById("new-name"); var emailInput = document.getElementById("new-email"); var noInput = document.getElementById("new-number"); var zipInput = document.getElementById("new-zip");

    var resultInput  = nameInput + " " + emailInput + " " + noInput +  " " + zipInput ; 
    // [object HTMLInputElement] [object HTMLInputElement] null [object HTMLInputElement]
 // it should not be null (should carry value)

    var resultInput = nameInput.concat(emailInput); 
//Uncaught TypeError: nameInput.concat is not a function(anonymous function) @ app.js:10

</script>

please help

8 Answers

Byron Stine
Byron Stine
4,877 Points

I have been working on your code. It was rough for me to figure out the delete part, but I figured it out. I did a for loop for adding the input values to the list. Check out my code on jsfiddle: (http://jsfiddle.net/r8n05d80/)

thanks you ... now i need to validate inputs in javascript as text , mail , phone no and number. Asloo i need to add a popup each time i add / delete a line of code . wish me good luck causei need it . thx you again

YI LI
YI LI
4,094 Points

Hi Eli concat() can be used for string and array. Your nameInput and emailInput are formatted in object. I think it is why your code doesn't work.

hope it helps.

yes of course ... it have somebody any idea what can i do to concatenate these objects ?

Zoltán Holik
Zoltán Holik
3,401 Points
    var nameInput = document.getElementById("new-name").value; 
    var emailInput = document.getElementById("new-email").value; 
    var noInput = document.getElementById("new-number").value; 
    var zipInput = document.getElementById("new-zip").value; 

    var resultInput  = nameInput + " " + emailInput + " " + noInput +  " " + zipInput ;

// [object HTMLInputElement] [object HTMLInputElement] null [object HTMLInputElement]

THATS NOT WORKING . IT THROWS ME // [object HTMLInputElement] [object HTMLInputElement] null [object HTMLInputElement]

Byron Stine
Byron Stine
4,877 Points

The reason you are getting the results [object HTMLInputElement] when you return the value of your variables is because your are only accessing the ID of the element. You have to access the innerHTML of that ID. This is done using .innerHTML at the end of your getElementById() method. Example:

 var nameInput = document.getElementById("new-name").innerHTML; 
    var emailInput = document.getElementById("new-email").innerHTML; 
    var noInput = document.getElementById("new-number").innerHTML; 
    var zipInput = document.getElementById("new-zip").innerHTML;

var resultInput  = nameInput + " " + emailInput + " " + noInput +  " " + zipInput ;

Byron Olson-Stine could you help me with the folowing code in js please ? http://jsfiddle.net/u27Lytqt/

its the whole script there . Right now I can add only one variable to the list instead 4 . Thanks in advance . Trying a lot to solve the problem by myself

Byron Olson-Stine could you help me with the folowing code in js please ? http://jsfiddle.net/u27Lytqt/

its the whole script there . Right now I can add only one variable to the list instead 4 . Thanks in advance . Trying a lot to solve the problem by myself but it doesn t work

Byron Stine
Byron Stine
4,877 Points

I'm looking over the code and it has to do with this code:

  if(taskInput.value) {
    cTasksHolder.appendChild(listItem);
    bindTaskEvents(listItem);
  } else {
    alert("You must enter the name , the email , the phone and the zip code.");
  }

Toward the top of you javaScript code you have this:

var nameInput = document.getElementById("new-name"); 
...
var taskInput = nameInput; 

Notice you have the if statement comparing the taskInput.value which you have assigned as the variable that holds the nameInput variable that holds the single object ID="new-name". Since this is a single item it's only adding a single item to the list. I would put your input objects into an array and then incorporating a for loop around your if statement that cycles through the array to add them to the list. With more time I can come up with some example code. But hopefully this will get you in the right direction.

i modified the array . http://jsfiddle.net/u27Lytqt/1/ still need help or some directions if that's possible. Thanks you very much

I ve changed var taskInput ¬javascript¬¬ var taskInput = [nameInput , emailInput , noInput, zipInput];

it seems that I have now to grab each element and output and i m wondering a bit how cause I have var createNewTaskElement = function(taskString) { ...... label.innerText = taskString; ... }