The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> 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
post 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>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 1 2022, 07:21 AM
Post #2


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



Seems the headline got chopped off, so could describe the problem again?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
fllwxDumbian
post 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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jan 1 2022, 09:14 AM
Post #4


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
fllwxDumbian
post 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 biggrin.gif
//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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jan 1 2022, 10:28 AM
Post #6


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
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. biggrin.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
fllwxDumbian
post 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!!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th April 2024 - 05:46 PM