AlexaS
AlexaS

Reputation: 3

Storing part of html page in a variable

I have some HTML like this:

<div class="col-xs-3 col-sm-3 col-md-3 no-padding--xs" style="text-align:right; padding-left:30rem">
<div class="no-padding--xs box--lang">
<a href="Intranet_sk.html"><img style="width:30px" src="Intranet_flags/flag-SVK-round.png"></a>
<a href="Intranet_en.html"><img style="width:30px"src="Intranet_flags/flag-UK-round.png"></a>
</div> 
</div>
</div>

and I want to keep it in the upper part of page layout but use it in the lower part of the (rendered) page, a bit like if you store the code in a variable and use the variable in specific part of the page.

The whole Intranet will be made of many modules but this one will be unique for every "subpage" so I want to keep the editable code in the upper part of the page layout and call/publish it using a lower static module.

Why? the "Intranet_" in href part will be unique for every subpage.

Alternatively, could I attach "_sk" or "_en" to the subpage metaname and create the link like that?

Upvotes: 0

Views: 76

Answers (2)

KooiInc
KooiInc

Reputation: 122956

  1. Create an element (document.createElement)
  2. Append clones of the necessary elements to that ([createdElement].appendChild([someElement].cloneNode([true])))
  3. Reuse whatever you need from within the element created ([createdElement].querySelector[All](...))

Essential here: the created element only exists in memory (is not inserted/appended in the DOM).

Upvotes: 0

Shivanshu Kant Prasad
Shivanshu Kant Prasad

Reputation: 170

You can store html code in a normal string and add it to any html element by doing element.innerHTML += stringThatContainsHTML

You can also retrieve HTML in the same way var htmlString = element.innerHTML

Upvotes: 1

Related Questions