Home > Net >  How to insert an element value - using jQuery - into an alert box
How to insert an element value - using jQuery - into an alert box

Time:06-06

I am trying to add the Strong element to the alert box in this scenario. However it is returning "undefined".

Can someone let me know where I have gone wrong with my syntax here. Thanks

$('.copyURL').click(function(e) {
  e.preventDefault();
  let ele = $(this).parent().siblings(':first-child').children();
  let label = $(this).parent().parent().siblings(':first-child').children();
  ele[0].select();
  ele[0].setSelectionRange(0, 99999);
  navigator.clipboard.writeText(ele[0].value);
  alert(label[0].value   "\r\n"   "\r\n"  
    "Copied: "   ele[0].value   "\r\n"   "\r\n"   "Use this link for your WEB site");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <label>
      <strong>Copy This Strong Label Into Alert</strong>
  </label>
  <div >
    <div >
      <input type="text" value="<#servername>/<#Database_Name>/<#business_business>/gift" readonly>
    </div>
    <div >
      <a  href="./<#business_business>/gift" target="_blank">View</a>
    </div>
    <div >
      <a ><i ></i> Copy URL</a>
    </div>
  </div>
</div>

CodePudding user response:

You need to use textContent to get the text from the strong element, not value:

$('.copyURL').click(function(e) {
  e.preventDefault();
  let ele = $(this).parent().siblings(':first-child').children();
  let label = $(this).parent().parent().siblings(':first-child').children();
  ele[0].select();
  ele[0].setSelectionRange(0, 99999);
  navigator.clipboard.writeText(ele[0].value);
  alert(label[0].textContent   "\r\n"   "\r\n"  
    "Copied: "   ele[0].value   "\r\n"   "\r\n"   "Use this link for your WEB site");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <label>
      <strong>Copy This Strong Label Into Alert</strong>
  </label>
  <div >
    <div >
      <input type="text" value="<#servername>/<#Database_Name>/<#business_business>/gift" readonly>
    </div>
    <div >
      <a  href="./<#business_business>/gift" target="_blank">View</a>
    </div>
    <div >
      <a ><i ></i> Copy URL</a>
    </div>
  </div>
</div>

  • Related