0

So I have an array in my script.js file. The array contains around 12 different things. I use the array to store divs IDs'. Because I want to load those divs dynamically. I've done that I loaded the divs dynamically but now I want to use that array for loading things inside the first div(a title a picture and so on).

let donorFeatureNames = [
    'SpawnVehicle',
    'RepairVehicle',
    'RocketVoltic',
    'MoreVehicle',
    'ChatColors',
    'Deagle',
    'M4',
    'Sniper',
    'CopFeature',
    'CrimFeature',
    'Changeskin',
    'Cash'
]

function loadFeatures () {
    for (i = 0; i < 12; i++) {
        $('#featureMenu').append('<div id=' + '"' + donorFeatureNames[i] + '"' + 'class="item notLoaded"></div>')

        $("'#" + donorFeatureNames[i] + "'").append(span class="title">' + $(this).data('donorfeature') + '</span>)
    }

I hope you understand what I'm asking. Cause I'm not that good at explaining things.

Fokkess
  • 1
  • 1
  • Do you want to add these additional properties during or after instantiation? – Korgrue Jun 05 '17 at 17:14
  • I've updated the post. Main concern is how to get this part to work: $("'#" + donorFeatureNames[i] + "'") Everything else I would probably know how to do. – Fokkess Jun 05 '17 at 17:16
  • `$(this).data('donorfeature')` what is `this`? Are you calling this function inside of a click handler? Also `$("'#" + donorFeatureNames[i] + "'")` should just be `$("#" + donorFeatureNames[i])` and the second `append` quotations are all off – Huangism Jun 05 '17 at 17:21
  • Thanks a lot! It works now. The $("'#" + donorFeatureNames[i] + "'") was all wrong, I re-wrote it using your comment. Thanks a bunch for helping me out. – Fokkess Jun 05 '17 at 17:32

2 Answers2

0

Instead of an array, just use jquery:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index1001</title>
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        //credit to https://stackoverflow.com/questions/941206/jquery-add-image-inside-of-div-tag
        $(function () {
            $("#test1").append("Text");
            $('#test2').prepend("<img src='../../Images/w.JPG' />")
        })
    </script>
</head>
<body>
    <div id="test1" class="anArray"></div>
    <br/>
    <div id="test2" class="anArray"></div>
</body>
</html>

You can even do this to make an array: $(".anArray").addClass("aColor");

kblau
  • 2,094
  • 1
  • 8
  • 20
0

Huangism answered my question. $("'#" + donorFeatureNames[i] + "'") isn't correct, but this is: $("#" + donorFeatureNames[i])

That's basically what I asked. Thanks a lot man!

Fokkess
  • 1
  • 1