0

In my script there is a list of words that populate a grid dynamically.

Is there a way to fill the list with the words that populate the grid through HTML?

I have this at the moment...

var listOfWords = ["mat", "cat", "dog", "pit", "pot", "fog"];

Is there a way to add more words in this list through HTML rather than in the script?

I ask because new words need to be added by colleges that can deal with HTML but not script

Thanks.

2 Answers 2

1

In theory, you could create an HTML structure that would create a DOM that could be parsed by JavaScript which would extract text from between certain elements (e.g. list items) and append those strings to the array.

… teaching people the very simple array syntax would be easier though.

… as would using a text file with one entry per line.

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

8 Comments

Could I have a list of divs in the HTML that hold the words and a method in the script to pull them out into the list, to then populate the grid? @Quentin
What would be your suggestion, bearing in mind that I cannot teach them the array syntax because I have been told so? @Quentin
@m0onio — Yes. A div wouldn't be as semantically appropriate as a list, but it would work in exactly the same way.
@m0onio — I'd suggest replacing management with one that recognised that learning JS array syntax is easier then writing HTML.
One last thing is that it wont randomize when I change the number to be produced? @Quentin
|
0

If someone is able to write this in html:

<ul>
    <li>hello</li>
    <li>hi</li>
    <li>hey</li>
</ul>

You can add them to list of words with:

[].push.apply( listOfWords, $("ul li").map( function() {
    return $(this).text();
}).get() );

console.log( listOfWords );
//["mat", "cat", "dog", "pit", "pot", "fog", "hello", "hi", "hey"]

Though I can't understand how could they not be able to write:

["hello", "hi", "hey"] 

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.