简体   繁体   中英

Bookmarklet JavaScript - add a character X to existing contents of web page element

The code I have below does the copy and flashes a brief confirmation message. I need it to instead add a character X in front of existing contents of that web page element:

<< ? - if you don't know just walk on by, if you need clarification - ask

 javascript:(function() { var copyText = document.getElementById("mergeFields-input-text"); copyText.select(); document.execCommand("Copy"); tempAlert("COPIED SUBJECT", 500); function tempAlert(msg, duration) { var el = document.createElement("div"); el.setAttribute("style","position:absolute;top:1%;left:35%;background-color:white;"); el.innerHTML = msg; document.body.appendChild(el); setTimeout(function(){ el.parentNode.removeChild(el); }, duration ); } })();

11.1.18 Version

 javascript:(function() { var copyText = document.getElementById("mergeFields-input-text"); copyText.innerHTML = "-" + copyText.innerHTML(); tempAlert("ADDED", 500); function tempAlert(msg, duration) { var el = document.createElement("div"); el.setAttribute("style","position:absolute;top:1%;left:35%;background-color:white;"); el.innerHTML = msg; document.body.appendChild(el); setTimeout(function(){ el.parentNode.removeChild(el); }, duration ); } })();

11.1.18 V2

 javascript:(function() { var copyText = document.getElementById("mergeFields-input-text"); copyText.value = "-" + copyText.value(); tempAlert("ADDED", 500); function tempAlert(msg, duration) { var el = document.createElement("div"); el.setAttribute("style","position:absolute;top:1%;left:35%;background-color:white;"); el.innerHTML = msg; document.body.appendChild(el); setTimeout(function(){ el.parentNode.removeChild(el); }, duration ); } })();

innerHTML isn't a function, it's a property that contains the current contents of DOM element. So you shouldn't have () after it.

 <a href='javascript:(function() { var copyText = document.getElementById("mergeFields-input-text"); copyText.value = "-" + copyText.value;})()'>Click me</a> <input id="mergeFields-input-text" value="initial contents">

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