How do I creat a JavaScript code to replace a name for a JavaScript code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I creat a JavaScript code to replace a name for a JavaScript code?

Like this one but instead of replacing a url, it should replace a name for a JavaScript code. <script type="text/javascript"> var elements = document.getElementsByTagName("a"); for(var i=0;i<elements.length;i++){ elements[i].href = elements[i].href.replace("google.com","google.net"); } </script>

28th Dec 2017, 8:32 PM
Jardiel Santos
Jardiel Santos - avatar
8 Answers
+ 5
Maybe something along these lines? https://code.sololearn.com/WoVWth9Mp702/?ref=app
31st Dec 2017, 12:37 AM
Kirk Schafer
Kirk Schafer - avatar
+ 5
To retrieve the div, query document.foo(), where foo() is: .getElementById("?"); // single result .getElementsByClassName("?"); // array .getElementsByTagName("div"); // array If you get an array, loop to find the right element: for(var el in retrievedArray) // scan retrievedArray[el].innerHTML for keyword To find keyword... http://www.dyn-web.com/javascript/strings/search.php Then replace: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
1st Jan 2018, 6:09 PM
Kirk Schafer
Kirk Schafer - avatar
0
but how is the script going to be like?
28th Dec 2017, 8:54 PM
Jardiel Santos
Jardiel Santos - avatar
0
but this way it won't replace anything, I want to replace a text for a JavaScript code.
28th Dec 2017, 9:17 PM
Jardiel Santos
Jardiel Santos - avatar
0
because I want to add this script only on pages where there is this specific name.
31st Dec 2017, 12:43 AM
Jardiel Santos
Jardiel Santos - avatar
0
Thanks for trying to help but using this way it will only replace a script for another script, I need a script to find a name inside a div tag and then replace this name for a script which is this Script <script type="text/javascript"> var adlinkfly_url = 'https://shrtner.ga/'; var adlinkfly_api_token = '1cb03faa96023ceab3d9628251998b2effa53094'; var adlinkfly_advert = 2; var adlinkfly_domains = ['depositfiles.com', 'uploading.com', 'uploadable.ch']; </script> <script src='//shrtner.ga/js/full-page-script.js'></script>
31st Dec 2017, 12:43 AM
Jardiel Santos
Jardiel Santos - avatar
- 1
If you want to replace the link text, which the user can see. Try element[i].innerHTML.
28th Dec 2017, 8:48 PM
Viereck
Viereck - avatar
- 1
Maybe this works: elements[i].innerHTML = "Text I want to be now here." Instead of: elements[i].href = elements[i].href.replace("google.com", "google.net"); I hope this is what need.
28th Dec 2017, 9:07 PM
Viereck
Viereck - avatar