0

I have a social media app that returns a list of different posts. Each post has an 'Add Comment' function. This requires the input to have an id field, but I know this needs to be unique, and as there are multiple posts that are dynamically generated I cant just change this.

I decided to concatenate the id with the post id like so:

items.push('<input type="text" class="commentContent" id="commentContent' + val["id"] + '" placeholder="Add A Comment..."></input>');

// Main part
id="commentContent' + val["id"] + '"

This means that each post has the following id attributes:

// Post 1
id="commentContentJTJmY2xpY2tjb250YWluZXIlMmY2Mzc3NzAyNjIzODE1MTQ2MjY="

// Post 2
id="commentContentJTJmY2xpY2tjb250YWluZXIlMmY2Mzc3Njk4NzYyOTgwNTQ4Njg="

And this is what I wanted, the issue is when I am trying the value of the input from post 1 for example. I am trying to access it like this:

$("#" + commentInput).val()

// Console log
customID = "#" + commentInput;
console.log(customID);

// Result
#commentContentJTJmY2xpY2tjb250YWluZXIlMmY2Mzc3NzAyNjIzODE1MTQ2MjY=

But this gives me the following error:

Error: Syntax error, unrecognized expression: #commentContentJTJmY2xpY2tjb250YWluZXIlMmY2Mzc3NzAyNjIzODE1MTQ2MjY=

Even this is the correct ID??

I have tried all the solutions from this question here, but none of them have worked.

Please help!

rc07jnr
  • 49
  • 6
  • 1
    `id='commentContent' + val["id"]` – Rafael Herscovici Jan 06 '22 at 01:26
  • @Dementic unfortunately that didn't work due to the way I have my quotes in the `items.push()` - they start with `items.push(' ')` so when I change to the above it gives me an error. I changed it to this: `items.push('');` – rc07jnr Jan 06 '22 at 01:30
  • `id="commentContent'" + val["id"] + "'"` – Rafael Herscovici Jan 06 '22 at 01:32
  • The issue is the `=` strip that out from your ID tags as they will only cause problems. https://stackoverflow.com/questions/12208390/jquery-selector-contains-equals-sign – Kinglish Jan 06 '22 at 01:41
  • Make use of the tools jQuery gives you to avoid nested quote issues... `items.push($("", { type: "text", "class": "commentContent", id: \`commentContent${val.id}\` }))` – Phil Jan 06 '22 at 01:41
  • @Phil I added this code, but it just outputs `[object Object]` – rc07jnr Jan 06 '22 at 02:37
  • I don't know what you're doing with that array but keep in mind that the jQuery method produces a `jQuery` object wrapping an HTML element, not a string – Phil Jan 06 '22 at 02:45

0 Answers0