简体   繁体   中英

Insert string to button onclick location.href Javascript

I have a button like this:

<button id="sync"  type="button" onclick="location.href = 'http://localhost/manage/test_yeezy/yooo/massSync/key/23ds31231ada/';" data-ui-id="sync-button">
    <span>Sync All</span>
</button>

i want to insert a string /listing_id/45/ like this after the massSync/ , the url can be dynamic, is there a way to do this?

You could accomplish this in the following way ...

 var link = 'http://localhost/manage/test_yeezy/yooo/massSync/key/23ds31231ada/'; var newLink = link.replace(/(massSync)/, '$1/listing_id/45'); $('#sync').attr("onclick", "location.href = '" + newLink + "'"); var location_href = $('#sync').attr('onclick').replace('location.href = ', ''); console.log(location_href); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="sync" type="button" onclick="location.href = 'http://localhost/manage/test_yeezy/yooo/massSync/key/23ds31231ada/';" data-ui-id="sync-button"> <span>Sync All</span> </button> 

** assuming massSync is static

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM