+ 2
How can I develop a javascript function that randomly chooses between the element in my array then link it to my text on html?
Function and linking
25 odpowiedzi
+ 5
let arr= [1,2,3,4,5,6,7,8,9,0];
document.getElementById("text_to_link");
let text = text_to_link;
text.innerText =
arr[ Math.round(Math.random()*(arr.length-1)) ]
+ 3
Cyrena
let arr = ["www.sololearn.com", "www.google.com"]
let tag = document.getElementsByTagName("a")[0];
tag.innerText =
arr[Math.round( Math.random()*(arr.length-1) ) ];
//😁😁😁😁
+ 3
Ex:
let result = MathRandom();
let elem = document.querySelector("a");
elem.setAttribute("href","result");
the link provided in previous answer will give you more details.
+ 3
(Still PM problem).
You're welcome, you're good to go so.👌🏻.
Thanks, you also stay safe 🌞🕊️
+ 2
RandomEl=arr[Math.floor(Math.random()*arr.length)]
+ 2
Cyrena ,
Once you have your result, you pass it as a attribute value of your element
yourElement.setAttribute("href","your random value");
https://www.tutorialrepublic.com/javascript-tutorial/javascript-dom-get-set-attributes.php
https://developer.mozilla.org/fr/docs/Web/API/Element/setAttribute
+ 2
Did you get my last pm which start with "To access myLink values put rani ..." ?
It seems connection problems are within pm function.
+ 2
To access myLink values put rani which is a number, in square brackets : myLink[rani] , check if your sites adresses are string type and in good format, and I don't remember if window.location is enough, if not try adding href : window.location.href
Add also a console.log(myLink[rani]) to check if the address is really picked up from the array.
Also is that the autocorrect went bad or a typo from you? => html => onclick="ranSite()" but in js => function randsite(){}
+ 2
😄 welcome Ann Dickson ,
I think in your enthusiasm you might have post not exactly where it suits 😁 or where you would like.
So I seize the opportunity to give you the link to a code from 👑 Shewe Uchi K 👑 to drive beginners with the Sololearn rules , good practices and advices.
Enjoy🌞, and welcome again.
https://code.sololearn.com/WykoN7bcDsgX/?ref=app
+ 2
Cyrena you are not the only one...
I am also facing another extra problem 😵
My profile name and picture has been changed to another person's 😵
+ 1
Cyrena I try to reply to your pm, but the message won't send, so :
I'm not sure I understand what you're trying to do.Would you post your code?
If the result is passed to href , it won't be visible normally.
If I understand, you have an array of links.You'd like the click to pick up a random link from that array, pass it to the href then it will redirect to the page/link...
If so, you'll have to add an eventListenner (like "onclick" on the element) and build a function for it so when you click first you pick up the link from the array, second it will pass it as a href as in the exemple, and then let the redirection operate.Maybe you'll have to handle something with a Promise to be sure the href is well provided with a value before the redirection...
You can also make use of window.location.href on whatever element , not only <a>
https://stackoverflow.com/questions/7077770/window-location-href-and-window-open-methods-in-javascript
+ 1
No I didn't get you message
+ 1
Woooi hope i can do it and i dont know how
+ 1
EmmanueLZ, ok. I think there's still a PM problem 'til now. Can't send a message
0
۰۰لگر🥰
0
Cyrena , sorry to reply you here but my PM section still have issue.
It seems you just asked me today for some help but the link you provided isn't found when I click on it.
Would you share it here, maybe it will work from that section...
Cheers.🙂
0
Cyrena hi,
(Still PM message issue for me, I can receive but not reply).
So about your last PM message:
Not sure but I think you can try writing it without the g variable.
As it is you store the goBack function in the variable without using "g" later.
Try :
document.getElementById("press").addEventListener("click",goBack);
Or :
var g=document.getElementById("press");
g.addEventListener("click",goBack);
Let me know if it works or not.🙂
0
Oh, ok, on your computer so?
The two folders Name and Pages are both in the same wrapper folder ?
>relative path including the folder name =>
window.location.href="../name/name.html".
Do you have an error message when you check the console Dev tools of your browser ? What is it saying ?
0
I corrected the previous message...
but check the error message in Dev tools.
0
No, the main folder is name.
So, Name(main folder)
->name.html
->pages(folder)
->hello.html