Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<div id="main1">
    <span class="username">Username_1</span>
    white some text content inside div...
    blue some text content inside div...
    yellow some text content inside div...
</div>
<div id="main2">
    <span class="username">Username_2</span>
    another test1 text content inside div...
    another test2 text content inside div...
    another text test3 content inside div...
</div>
  
  <button onclick="getSelectedSpan()">Get Span</button>
</body>
</html>
 
function getSelectedSpan() {
  var baseNode = window.getSelection().baseNode;
  var nearestParentDiv = baseNode.parentNode.closest("div");
  var spanList = nearestParentDiv.getElementsByTagName("span");
  console.log(spanList[0].innerHTML);
}
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers