*EDIT
I have a list in my HTML called “#wordlist”. In this list I hold the words in my game, the attached audio and the attached image to each word. This all works fine.
Because there will be different versions of the game, I have been asked to take some of the variables capable of changing the amount of words to complete, size of the grid etc; and place them as data-attributes in the list. (Making all the editing factors available in one place).
The problem is I can’t get my head around how I am going to type a value in for something like “number-input” below and that changing the variable “numberInput” in the script.
Is this possible to do?
Here is the list (data-word, data-audio and data-pic are all OK)
<ul id="wordlist">
<li data-audio="" data-pic="images/one.png" data-word="one" data-number-input="" data-completion-number"" data-grid=""></li>
<li data-audio="" data-pic="images/two.png" data-word="two" data-number-input="" data-completion-number"" data-grid=""></li>
<li data-audio="" data-pic="images/three.png" data-word="three" data-number-input="" data-completion-number"" data-grid=""></li>
<li data-audio="" data-pic="images/four.png" data-word="four" data-number-input="" data-completion-number"" data-grid=""></li>
<li data-audio="" data-pic="images/five.png" data-word="five" data-number-input="" data-completion-number"" data-grid=""></li>
<li data-audio="" data-pic="images/six.png" data-word="six" data-number-input="" data-completion-number"" data-grid=""></li>
<li data-audio="" data-pic="images/seven.png" data-word="seven" data-number-input="" data-completion-number"" data-grid=""></li>
<li data-audio="" data-pic="images/eight.png" data-word="eight" data-number-input="" data-completion-number"" data-grid=""></li>
<li data-audio="" data-pic="images/nine.png" data-word="nine" data-number-input="" data-completion-number"" data-grid=""></li>
<li data-audio="" data-pic="images/ten.png" data-word="ten" data-number-input="" data-completion-number"" data-grid=""></li>
</ul>
Here are the variable that I need to change through the data-attributes in the HTML
var numberInput = 2;
var completionNumber = 2;
var smallGrid = {
x: 4,
y: 4
};
var largeGrid = {
x: 8,
y: 6
};
So basically So if "data-number-input="2" this would make the variable "numberInput = 2;" in the script
First of all, your
htmlwasn’t good at all, you must change it to this:(
dataattributes were corrected…)HTML:
Then, to assign new values to the
dataattributes, use the following. In this case I just affected the firstlibut you can do aloopor.map()in jQuery if you want to change all of them.Live DEMO: http://jsfiddle.net/oscarj24/qWyj6/1/
jQuery code:
Changing all
liattributes:just do this using jQuery, this is like the “equivalent” of a
loop:Live DEMO: http://jsfiddle.net/oscarj24/qWyj6/2/
(In live demo check browser’s console to see the output)
If you want to get the
dataattribute from aliand put in a variable, just do this:But, If you want to get all
dataattributes from allliand put them in variables or javascriptobject, just do this:Live DEMO: http://jsfiddle.net/oscarj24/qWyj6/3/
This will be the output:
And, to get for example the
number-inputvalue from the firstliof thisobject, just do this:Hope this helps 🙂