I'm new to HTML and i need feedback on something, Trying to switch images on button click but so when u click it again i |
I'm new to HTML and i need feedback on something, Trying to switch images on button click but so when u click it again i |
fllwxDumbian |
Jan 1 2022, 04:40 AM
Post
#1
|
Group: Members Posts: 6 Joined: 1-January 22 Member No.: 28,210 |
<script>
function aclick() { var xy; var xy = 0; if (xy === 0) { document.getElementById('abc') .src="D:\\FLoW Dumbian\\pics\\healer.png"; var xy; var xy = 1; } else if (xy === 1) { document.getElementById('abc') .src="D:\\FLoW Dumbian\\pics\\electrician.png"; var xy; var xy = 0; } } </script> |
Christian J |
Jan 1 2022, 07:21 AM
Post
#2
|
. Group: WDG Moderators Posts: 9,722 Joined: 10-August 06 Member No.: 7 |
Seems the headline got chopped off, so could describe the problem again?
|
fllwxDumbian |
Jan 1 2022, 07:46 AM
Post
#3
|
Group: Members Posts: 6 Joined: 1-January 22 Member No.: 28,210 |
Hey, so i want to change image whenever i press a button
xy = 0 -> if = true and the image changes, at the end it sets xy = 1 and the next time i click the button if = false cause xy is 1 not 0 and skips to else vica verse |
pandy |
Jan 1 2022, 09:14 AM
Post
#4
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,753 Joined: 9-August 06 Member No.: 6 |
You logic is off. xy will be 0 every time the button is clicked since you set it to 0 in the beginning of the function.
Then maybe you shouldn't use Windows paths and they shouldn't contain spaces. That xy thing can be avoided. Look up getAttribute(). An example... CODE function switchIt() { if (document.getElementById('foo').getAttribute('src') === 'a.jpg') document.getElementById('foo').setAttribute('src', "b.jpg"); else document.getElementById('foo').setAttribute('src', "a.jpg"); } getAttribute('src') will return the URL to the image that's actually loaded while if you used just getElementById('foo').src the URL that's printed in the source would be ruturned. Sorry, I didn't bother to change to the names you use for the function and id. |
fllwxDumbian |
Jan 1 2022, 10:12 AM
Post
#5
|
Group: Members Posts: 6 Joined: 1-January 22 Member No.: 28,210 |
You're right. I feel so dumb. Thank you for clarifying it for me. jesus
//edit: I'm not using any tutorials and i'm learning html for three days. I don't know lots of stuff yet, thanks for your help. This post has been edited by fllwxDumbian: Jan 1 2022, 10:18 AM |
pandy |
Jan 1 2022, 10:28 AM
Post
#6
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,753 Joined: 9-August 06 Member No.: 6 |
Then you've come a long way. I didn't even dare to look at JavaScript until the fourth day.
|
fllwxDumbian |
Jan 1 2022, 10:42 AM
Post
#7
|
Group: Members Posts: 6 Joined: 1-January 22 Member No.: 28,210 |
What can i say, i'm a quick study but my goal is to make a game and this is for my character selection :'D. Anyway, have a nice day and thank u a lot!!
|
Lo-Fi Version | Time is now: 26th September 2024 - 01:56 AM |