Home > Software engineering >  How to get an element that was created during page load?
How to get an element that was created during page load?

Time:11-30

I'm creating elements dynamically based on SQL data server side. I want to also edit some of those element's attributes after their creation.

They way I'm trying to go about this is by generating a string with the elements and inserting it into a div's innerHTML:

client side:
<div id=master runat="server"></div>

server side:
string textToDiv = "<div id='"  num  "'><ul><li></li><li></li></ul></div>";
master.innerHTML = textToDiv;

Looks something like this in chrome:

<div id="master" runat="server">
  <div id='1'>
    <ul>
      <li></li>
      <li></li>
    </ul>
  </div>
  <div id='2'>
    <ul>
      <li></li>
      <li></li>
    </ul>
  </div>
</div>

Now, I want to change one of the child div's attributes. How do i go about doing that?

All I found on the internet is for more static uses using the method I applied when changing the 'master' div's innerHTML attribute. Is there a get() function or something similar to document.getElementByID() I can use?

CodePudding user response:

a better way to solve this, to just simply place your script under the html code, but still in the body, this will make sure, the script is just loaded, when everything was rendered to the screen, with this, you can instantly access the elements and you dont have to use the window.onload eventlistener

CodePudding user response:

I think you need to use the onload event and then use a function for create the Element.

window.onload() => {
    element = document.createElement(" ");
    element.setAttribute(" ");
    element.innerHTML = ' ';
    document.getElementById(' ').appendChild(element);
}

I hope it's can help you

  • Related