Search code examples
javascriptjqueryhrefreplacewith

window location href inside jquery replaceWith tag


thanks for the amazing community, i'm strugling with this code i'm tring to load href link with window.location.href + string '/1/' into my jquery replaceWith tag but no luck could you help me please this is my code:

var myhref=window.location.href+'/1/':
$j('.the-tag p').replaceWith('<a href="'.myhref.'">My Link</a>');

thank you for your help again


Solution

  • Are you trying to do something like below ? You need to include jquery library files, and then below will work. In the code I have fixed : at the end of line 1 to ;

    var myhref = window.location.href + '/1/';
    $('.the-tag p').replaceWith('<a href="' + myhref + '">My Link</a>');
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="the-tag">
      <p></p>
    </div>