Solution 1 :

I think you forgot to trim off some part of the id of the div-element.

Changing your first line to let postNumber = $("div[id^='product-']").attr("id").replace('product-', ''); would allow you to use postNumber as the key (.href(foo[postNumber]);).

Also, I think then your selection would work as you intended.

Problem :

Here is the JS in question (with as just an example):

 let postNumber = $("div[id^='product-']").attr("id");
const productId = "product-",
            AWS = "";

let foo = {

    "28162":  AWS + "R3CRDT_DIMTOWARM_SPSHT.pdf",
    "28163":  AWS + "R3CRPT_DIMTOWARM_SPSHT.pdf",
    "28164":  AWS + "R3CRWT_DIMTOWARM_SPSHT.pdf",
    "28165":  AWS + "R3CRDL_DIMTOWARM_SPSHT.pdf",
    "28166":  AWS + "R3CRPL_DIMTOWARM_SPSHT.pdf",
    "28167":  AWS + "R3CRWL_DIMTOWARM_SPSHT.pdf",
    "28168":  AWS + "R3CRDT_DIMTOWARM_SPSHT.pdf",
    "28169":  AWS + "R3CRDT_DIMTOWARM_SPSHT.pdf",
    "28170":  AWS + "R3CRDT_DIMTOWARM_SPSHT.pdf",
    "28171":  AWS + "R3CRDT_DIMTOWARM_SPSHT.pdf",
    "28172":  AWS + "R3CRDT_DIMTOWARM_SPSHT.pdf",
    "28173":  AWS + "R3CRDT_DIMTOWARM_SPSHT.pdf"


  $("div[id='product-" + postNumber + "] ul#dropdown-download-links > li > ul > li:last-of-type").href(foo[]);

Keep in mind AWS is just the static URL for the files.

How would I get a dynamic version of the .href() link value using bracket notation with the object foo. As you can see I have the jQuery statement to retrieve the URL from the above object. I just can’t figure out the last part where I would enter .href(foo[something]); dynamically? Any ideas? Or maybe a better way og doingi t?


Comment posted by Andre Nuechter

Do the keys of

Comment posted by Taplar

Side Note; the bottom selector that you are using suggests that the

Comment posted by rec0nstr

@AndreNuechter: Yes they do