简体   繁体   中英

Cannot add Id tag using JQuery

I would like to add a custom id tag eg id = keys[key][each] as part of the html that's being appended to the new_resumes class eg I would like

<div class = "filtered_results" id = keys[key][each] ...

instead with the code below - when I use the attr or prop tag it adds an id to the new resumes class. I appreciate my code is not the greatest.

for (var key in keys) {
    $(".new_resumes").append("<div class='filtered_results' style='color:white; background-color:red; text-align:center; height:20px; line-height: 20px'>" + key + "</div>");
    for (var each in keys[key]) {
        $(".new_resumes").append("<div class='filtered_results' style ='height:60px; text-align:center; height:60px; line-height: 60px;'>" + keys[key][each][0] + "</div>").prop('id',keys[key][each][0]);

If I understand your question, you want to add the ID to the filtered_results div you are creating?

You should just concatenate it like so:

$(".new_resumes").append("<div class='filtered_results' id='" +keys[key][each][0] +"' style ='height:60px; text-align:center; height:60px; line-height: 60px;'>" + keys[key][each][0] + "</div>")

Take note of this part: id='" +keys[key][each][0] +"'

Right now, you are calling .prop on the current element which is still the .new_resumes element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM