Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Client-side Scripting _ I'm new to HTML and i need feedback on something

Posted by: fllwxDumbian Jan 1 2022, 04:40 AM

<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>


Posted by: Christian J Jan 1 2022, 07:21 AM

Seems the headline got chopped off, so could describe the problem again?

Posted by: fllwxDumbian Jan 1 2022, 07:46 AM

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

Posted by: pandy Jan 1 2022, 09:14 AM

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.

Posted by: fllwxDumbian Jan 1 2022, 10:12 AM

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.

Posted by: pandy Jan 1 2022, 10:28 AM

Then you've come a long way. I didn't even dare to look at JavaScript until the fourth day. biggrin.gif

Posted by: fllwxDumbian Jan 1 2022, 10:42 AM

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!!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)