Search code examples
javascripthtmlprototypejs

Inserting an id into a div using Prototype/JS


Is it possible to insert an id into a div using prototype/javascript? Basically:

<div> Hello </div>

should become:

<div id="myId"> Hello </div>

Thanks!


Solution

  • Yes, but without more code it's going to be tough to do because you have no way of identifying that <div> as it currently is coded. If it followed a <div> with and ID of somediv then you could do:

    $$('#somediv > div')[0].setAttribute('id', 'myId');