Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random commas in my HTML

I have commas between my HTML buttons and i don't know where they come from. The div in which i create the buttons is empty before the buttons are being created.

In my JavaScript, i don't have any commas that could be put between the buttons as you can see below.

Here is a picture:

enter image description here

I create the buttons with JavaScript like so:

    var f = "'";
    contents[0]='<button class="btn" onclick="check('+ f + randomChar0 + f +');">' + randomChar0 + '</Button>';
    contents[1]='<button class="btn" onclick="check('+ f + randomChar1 + f +');">' + randomChar1 + '</Button>';
    contents[2]='<button class="btn" onclick="check('+ f + randomChar2 + f +');">' + randomChar2 + '</Button>';

    ranNum0 = randomize(2);
    document.getElementById("response").innerHTML = contents;

My HTML looks like this before i insert the buttons:

<div id="response" class="center-children">
</div>

And like this after i insert the buttons:

<div id="response" class="center-children">
    <button class="btn" onclick="check('B');">B</button>,
    <button class="btn" onclick="check('S');">S</button>,
    <button class="btn" onclick="check('E');">E</button>
</div>

If i check in the Browser, it looks like this:

enter image description here

I checked my whole JavaScript, even tried to delete all the CSS and HTML but the commas remain there...

Does anyone have a clue what might cause this?

like image 670
00Gheist00 Avatar asked May 16 '15 19:05

00Gheist00


People also ask

How to format a number to a string with thousands of commas?

The toLocaleString method lets us format a number to a string with commas as thousands of separators automatically. You can do HTML input number format comma with it. const str = (1234567890).toLocaleString () console.log (str) Or using autoNumeric plugin you can make a field as numeric input with different separators.

How to add a comma to a string in HTML?

You can do HTML input number format comma with it. const str = (1234567890).toLocaleString () console.log (str) Or using autoNumeric plugin you can make a field as numeric input with different separators.

How to create random numbers in JavaScript?

JavaScript Random 1 Math.random (). Math.random () always returns a number lower than 1. 2 JavaScript Random Integers. Math.random () used with Math.floor () can be used to return random integers. There is no... 3 A Proper Random Function. As you can see from the examples above, it might be a good idea to create a proper random... More ...

How to get rid of comma at the end of list?

We use the general sibling selector to get rid of the comma at the end. Design the list using HTML ul using class name as students and then create li elements with class name student show. Apply the CSS to the given list.


Video Answer


2 Answers

The problem is that you are assigning the string value of your array to .innerHTML and the string value of an array has commas between the elements.

In order to combine the values in your array, use .join():

 document.getElementById("response").innerHTML = contents.join('');

Or better yet, don't use string manipulation to create DOM elements.

like image 76
JLRishe Avatar answered Oct 11 '22 18:10

JLRishe


You're setting innerHTML to be an array, but it's supposed to be a string. Implicitly, .toString() is being called on your array, which leaves commas. Try it out, [1,2,3].toString() evaluates to "1,2,3".

like image 43
Raphael Serota Avatar answered Oct 11 '22 18:10

Raphael Serota