cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <p>
  Click and drag to highlight elements below. The text will highlight and be seen in the form field and as a URL parameter.
</p>

<p>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Qua tu etiam inprudens utebare non numquam. Hoc est non dividere, sed frangere. Tecum optime, deinde etiam cum mediocri amico. Polemoni et iam ante Aristoteli ea prima visa sunt, quae paulo ante dixi. Non est igitur summum malum dolor. Quae cum praeponunt, ut sit aliqua rerum selectio, naturam videntur sequi; Duo Reges: constructio interrete. Cum autem in quo sapienter dicimus, id a primo rectissime dicitur.

Eadem fortitudinis ratio reperietur. Sed ea mala virtuti magnitudine obruebantur.
</p>

<form>
  Highlighted:<br>
  <input type="text" name="highlight" id="hi"><br> 
</form>

<a id="my-link" href=""></a>
            
          
!
            
              body {
  width: 700px;
  padding: 2em;
  margin: 0 auto;
}

#hi {
  width: 50%;
  height: 3em;
  display:block;
}

.active {
  margin: 2em 0;
  border: 1px solid;
  padding: .5em;
}

#my-link:hover {
  background-color: #efefef;
}
            
          
!
            
              // modified a bit from https://jsfiddle.net/timdown/SW54T/
//other thing used was https://stackoverflow.com/questions/5809310/jquery-how-do-i-apply-css-to-selected-text
//https://stackoverflow.com/questions/4232961/remove-a-html-tag-but-keep-the-innerhtml

var url = window.location.href;


//get highlighted text
function getSelectedText() {
        var text = "";
        if (typeof window.getSelection != "undefined") {
            text = window.getSelection().toString();
        } else if (typeof document.selection != "undefined" && document.selection.type == "Text") {
            text = document.selection.createRange().text;
        }
        return text;
    }
    

//set the form field and build the a href link
    function setField() {
        var selectedText = getSelectedText();
        var field = document.getElementById('hi');
        var link = document.getElementById('my-link');
        if (selectedText) {
            field.value = getSelectedText();
            link.href = 'https://foo.com/?highlight='+encodeURIComponent(selectedText)+'&url='+url; //builds URL
            link.innerHTML = "Submit";//sets text for the button
            link.classList.add("active"); //adds a class to the link to make it prettier
            highlightIt();
        }
    }
    
    document.onmouseup = setField;
    document.onkeyup = setField;


//adds/removes highlighting background color
function highlightIt(){
  var b = document.getElementsByTagName('span');
  
while(b.length) {
    var parent = b[ 0 ].parentNode;
    while( b[ 0 ].firstChild ) {
        parent.insertBefore(  b[ 0 ].firstChild, b[ 0 ] );
    }
     parent.removeChild( b[ 0 ] );
}

  var range = window.getSelection().getRangeAt(0);
  var newNode = document.createElement("span");
  range.surroundContents(newNode);
  newNode.style.backgroundColor = "yellow";
}
            
          
!
999px
Loading ..................

Console