I've got an html input in a div that looks like this:
<input type="text" class="form-control" placeholder="Barcode" role="barcode">
Using only JavaScript I need to add this into the DOM above it:
<span class="input-group-btn"><a class="btn btn-default" href="#"><i class="fa fa-barcode"></i> Scan</a></span>
So the end result is:
<div class="input-group">
<span class="input-group-btn"><a class="btn btn-default" href="#"><i class="fa fa-barcode"></i> Scan</a></span>
<input type="text" class="form-control" placeholder="Barcode" role="barcode">
</div>
I can't use appendChild on the div as the input-group class is not unique.
This is the JavaScript I've tried with no luck:
var barcodeField=document.querySelectorAll('[role=barcode]');
var span=document.createElement('span');
span.class='input-group-btn';
var scanButton=document.createElement('a');
scanButton.class='btn btn-default';
var linkText=document.createTextNode('Scan');
scanButton.href='#';
var i=document.createElement('i');
i.class='fa fa-barcode';
scanButton.appendChild(i);
scanButton.appendChild(linkText);
span.appendChild(scanButton);
barcodeField[0].appendChild(span);
For starters it adds it below the input (I know this is due to appendChild but appendParent doesn't exist and I just wanted to get it working). Also it appears to ignore the classes I've given each element, but the biggest problem is nothing shows up on the page.
The page is also using Bootstrap and JQuery.