Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Obtain an element with a dynamically generated ID

I have a very straightforward thing I am trying to do, but am new to Javascript.

I have an HTML page which generates a series of DIV elements with an ID that is dynamically generated.

Example: <div id="shots_234"></div>, <div id="shots_256"></div>

I am making a jQuery AJAX request, and I want to populate the html with the response targetting one of those IDs.

function WeddingsShotDown(shotid, segmentid) {

    document.getElementById('ShotDownId').value = shotid;

    var form = document.getElementById('ShotDown');
    var formData = new FormData(form);
    var id = "shots_" + segmentid;
    var divid = document.getElementById(id);

    console.log("Div: " + divid);

    $.ajax({
        url: '/Weddings/ShotDown',
        type: 'POST',
        data: formData,
        dataType: 'html',
        processData: false,
        contentType: false,
        success: function (response) {
            if (response) {
                $("#shots_" + segmentid).html(response);
                document.getElementById('ShotId').value = "";
                showNotification("success", "Your shot sequence has been updated", "far fa-check-circle");
            } else {
                showNotification("danger", "Error saving shot sequence", "far fa-check-circle");
            }
        }
    });
}

So neither

$("#shots_" + segmentid).html(response);

Nor

    var id = "shots_" + segmentid;
    var divid = document.getElementById(id);

Get me the div that I want.

How can I update that element?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  • Var segmentid not exist in the success function. Try, save id in the request with beforeSend, and get in the receive:
    $.ajax({
        url: '/Weddings/ShotDown',
        type: 'POST',
        data: formData,
        dataType: 'html',
        processData: false,
        contentType: false,
        beforeSend:function( jqXHR ){
            jqXHR.id = segmentid;
        }
    }).done(function(response, msgStatus, jqXHR){
        if (response) {
                $("#shots_" + jqXHR.id).html(response);
                document.getElementById('ShotId').value = "";
                showNotification("success", "Your shot sequence has been updated", "far fa-check-circle");
            } else {
                showNotification("danger", "Error saving shot sequence", "far fa-check-circle");
            }
    });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!