coding issue

Anything else. Post a funny site or tell us about yourself. Discuss current events or whatever else you want. Post off topic threads here.
Post Reply
User avatar
[SD]Master_Wong
Forum God
Posts: 9509
Joined: Wed Jan 04, 2006 8:02 pm
Quick Reply: Yes
Location: Plymouth, University

coding issue

Post by [SD]Master_Wong »

first off not asking anyone to do my work for me just help me work out why something isnt working.

The problem was after i removed the html code for the buttons in favor of a loop to create them, the id for each line created in the loop matchs that on the subroutines button name so it should work. but it dont for the life of me i cant work out why. all the buttons do come up on screen just do nothing although the code to disable and enable them seems to work so it can pick up the id names il also include that code as that code does work.

if i cant get this working i have to revert to other code which will mean i earn less marks but i cant understand why this isnt working

Here is the code in question also theres a dropbox link if anyone wants to use that.

Code: Select all

<html>
    <head>
        <title>Animal Garden</title>
    </head>
    <body background="background3.jpg">
        <center>
            <img src="Header.png" /><br />
            <div id="fuzzy" align="center"></div>
            <div id="star" align="center"></div><br />
            <input id="btnrandom" type="button" value="?" />     
            <p id="par1"></p>
            <p id="par2"></p>
        </center>
        <object id="animalsound" classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" style="width:0px; height:0px;"></object>
    </body>
</html>
<script language="vbscript">
option explicit
dim animal(7), previous, rand(1), button, correct(4), count(1), stars(5), num(1), green, red, stopstart

sub window_onload()
dim i, star, animals
        Do
            i = i + 1
            Star = Star & "<img id=star" & i & " " & "src=Star/StarOff.gif />"
        Loop Until i = 5
            document.getElementById("star").innerHTML = Star & document.getElementById("star").innerHTML   
        i = 0
        Do       
    'once solved work out if onclick=function() can use vbscript   
            i = i + 1
            animals = animals & "<button id='btnanimal" & i & "' style='padding: 0px 0px;'><img src='Animals/Animal" & i & ".jpg'" & " " & "width='200' height='150' /></button>"
        Loop Until i = 7
            document.getElementById("fuzzy").innerHTML = animals & document.getElementById("fuzzy").innerHTML
        disable
        count(0) = 0
        count(1) = 0
        par2.innertext = "Please Start By Clicking the ? Button"
        previous = "nothing"
        animal(1) = "Damselfly"
        animal(2) = "Frog"
        animal(3) = "Hedgehog"
        animal(4) = "Newt"
        animal(5) = "Slowworm"
        animal(6) = "Thrush"
        animal(7) = "Butterfly"
        stars(0) = "Star/StarOff.gif"
        stars(1) = "Star/StarOff.gif"
        stars(2) = "Star/StarOff.gif"
        stars(3) = "Star/StarOff.gif"
        stars(4) = "Star/StarOff.gif"
        stars(5) = "Star/StarOff.gif"
        green = "border: 8px solid green; padding: 0px 0px;"
        red = "border: 8px solid red; padding: 0px 0px;"
    end sub

    sub btnrandom_onclick()
        par1.innertext = ""
        par2.innertext = "Try Pick the Right Picture for this Animal"
        rand(0) = cint(rnd()*6+1)
        rand(1) = animal(rand(0))
        if rand(1) = previous then
                Do
                    rand(0) = cint(rnd()*6)
                    rand(1) = animal(rand(0))
                Loop until rand(1) <> previous
                previous = rand(1)
            else
                previous = rand(1)
        end if
        animalsound.URL = "Animals/Animal" & animal(rand(0)) & ".wav"
        btnrandom.disabled = "disabled"
        disable
        border       
    end sub

    sub border
    dim i
        Do
        i = i + 1
        document.getElementById("btnanimal" & i).style.border=""
        loop until i = 7
    end sub

    sub btnanimal1_onclick()
        num(0) = 1
        button = animal(num(0))
        rightwrong
    end sub

    sub btnanimal2_onclick()
        num(0) = 2
        button = animal(num(0))
        rightwrong
    end sub

    sub btnanimal3_onclick()
        num(0) = 3
        button = animal(num(0))
        rightwrong
    end sub

    sub btnanimal4_onclick()
        num(0) = 4
        button = animal(num(0))
        rightwrong
    end sub

    sub btnanimal5_onclick()
        num(0) = 5
        button = animal(num(0))
        rightwrong
    end sub

    sub btnanimal6_onclick()
        num(0) = 6
        button = animal(num(0))
        rightwrong
    end sub

    sub btnanimal7_onclick
        num(0) = 7
        button = animal(num(0))
        rightwrong
    end sub

    sub rightwrong()
        animalsound.URL = "Animals/Animal" & animal(num(0)) & ".wav"
        if button = rand(1)  then
                par2.innertext = "Correct"   
                document.getElementById("btnanimal" & rand(0)).style.border="thick solid green"
                count(0) = count(0) + 1
                stars(count(1)) = "Star/StarOn.gif"
                count(1) = count(1) + 1
            else
                document.getElementById("btnanimal" & num(0)).style.border="thick solid red"
                document.getElementById("btnanimal" & rand(0)).style.border="thick solid green"
                par2.innertext = "Incorrect"
                par1.innertext = rand(1)
        end if
        disable
        if count(0) = 5 then
        dim Response
                animalsound.URL = "Animals/Animal" & animal(num(0)) & ".wav"
                star5.src = "Star/StarOn.gif"
                par2.innertext = ""
                btnrandom.disabled = "disabled"
                par1.innertext = ""
                Response = MsgBox("Well Done You Have Finished The Game",0)

                If Response = vbok Then
                reset
                border                 
                End If
            else
                btnrandom.disabled = ""
        end if
        starpics
    end sub

    sub starpics
        star1.src = stars(0)
        star2.src = stars(1)
        star3.src = stars(2)
        star4.src = stars(3)
        star5.src = stars(4)
    end sub

    sub reset()
        rand(1) = ""
        num(0) = ""
        animalsound.URL = ""
        par1.innertext = ""
        btnrandom.disabled = ""
        count(0) = 0
        count(1) = 0
        par2.innertext = "Please Start By Clicking the ? Button"
        stars(0) = "Star/StarOff.gif"
        stars(1) = "Star/StarOff.gif"
        stars(2) = "Star/StarOff.gif"
        stars(3) = "Star/StarOff.gif"
        stars(4) = "Star/StarOff.gif"
        starpics
        disable
        disable
    end sub

    sub disable
    dim j
        if stopstart = "disabled" then
                stopstart = ""
           else
               stopstart = "disabled"
        end if
        Do
            j = j + 1
            document.getElementById("btnanimal" & j).disabled = stopstart
        loop until j = 7
    end sub


failing that here is a dropbox link
http://www.facebook.com/l.php?u=http%3A ... =LAQFVlWge
MaStEr
Image
credits zelzin ^^

User avatar
NuclearSilo
Forum God
Posts: 8834
Joined: Mon Aug 21, 2006 12:00 pm
Quick Reply: Yes
Location: Age of Wushu

Re: coding issue

Post by NuclearSilo »

use javascript... forget vbscript
Playing Age of Wushu, dota IMBA

User avatar
penfold1992
Senior Member
Posts: 4059
Joined: Sun Apr 22, 2007 9:48 am
Quick Reply: Yes
Location: Uranus

Re: coding issue

Post by penfold1992 »

i like using vb script... but im not really used to the way things are done in this code... especially loops. I am also not familiar with some of the functions you are using too =( sorry
Image

User avatar
[SD]Master_Wong
Forum God
Posts: 9509
Joined: Wed Jan 04, 2006 8:02 pm
Quick Reply: Yes
Location: Plymouth, University

Re: coding issue

Post by [SD]Master_Wong »

NuclearSilo wrote:use javascript... forget vbscript


not useful i was set this project strictly for vbscript

i ended up giving up after bee advised i wont gain marks for getting this work. but the problem is because the btn_onclick() does not work with dynamically created code

wish i could ask for help on something else but tbh everything works fine just trying to work out ways to shorten the code one guy in my class has got the code down to 130 lines im on 187 lines but this could take up to 500 or more with basic code so i figure 187 isnt bad
MaStEr
Image
credits zelzin ^^

User avatar
poehalcho
Elite Member
Posts: 6131
Joined: Mon Apr 30, 2007 3:32 pm
Quick Reply: Yes
Location: ┌(╬ಠ益ಠ)╯( ̄ー ̄)(ノ◕ヮ◕)ノ:・✧(╯°Д°)╯彡┻━┻ψ(`∇´)ψ(☞゚∀゚)☞¯\_(ツ)_/¯ლ(ಥ益ಥლ)ԅ༼ ◔ڡ◔༽งヽ༼ʘ̚ل͜ʘ̚༽ノᕕ(ᐛ)ᕗ( ͡° ͜ʖ ͡°)

Re: coding issue

Post by poehalcho »

[SD]Master_Wong wrote:
NuclearSilo wrote:use javascript... forget vbscript


not useful i was set this project strictly for vbscript

i ended up giving up after bee advised i wont gain marks for getting this work. but the problem is because the btn_onclick() does not work with dynamically created code


In C# there is a function called delegate to deal with it. try looking for a vbscript equivalent?
I'm sorry I can't be more helpful than that, I'm still a scrub at coding T_T
Image
Day[9] wrote:"Tea is a lot like gold expansions - it helps you kill people."
- Day[9] Daily 337 -

User avatar
[SD]Master_Wong
Forum God
Posts: 9509
Joined: Wed Jan 04, 2006 8:02 pm
Quick Reply: Yes
Location: Plymouth, University

Re: coding issue

Post by [SD]Master_Wong »

im not gona waste too much time on it as it wont give me any marks
MaStEr
Image
credits zelzin ^^

User avatar
NuclearSilo
Forum God
Posts: 8834
Joined: Mon Aug 21, 2006 12:00 pm
Quick Reply: Yes
Location: Age of Wushu

Re: coding issue

Post by NuclearSilo »

[SD]Master_Wong wrote:
NuclearSilo wrote:use javascript... forget vbscript


not useful i was set this project strictly for vbscript

i ended up giving up after bee advised i wont gain marks for getting this work. but the problem is because the btn_onclick() does not work with dynamically created code

If it's dynamically created, you got to reattach the event dynamically too.
document.getElementById(..).addEventListener(...)
Playing Age of Wushu, dota IMBA

User avatar
[SD]Master_Wong
Forum God
Posts: 9509
Joined: Wed Jan 04, 2006 8:02 pm
Quick Reply: Yes
Location: Plymouth, University

Re: coding issue

Post by [SD]Master_Wong »

NuclearSilo wrote:
[SD]Master_Wong wrote:
NuclearSilo wrote:use javascript... forget vbscript


not useful i was set this project strictly for vbscript

i ended up giving up after bee advised i wont gain marks for getting this work. but the problem is because the btn_onclick() does not work with dynamically created code

If it's dynamically created, you got to reattach the event dynamically too.
document.getElementById(..).addEventListener(...)


so i have to say addEventListener("btnanimal" & i & "()")

and that will work?
MaStEr
Image
credits zelzin ^^

User avatar
penfold1992
Senior Member
Posts: 4059
Joined: Sun Apr 22, 2007 9:48 am
Quick Reply: Yes
Location: Uranus

Re: coding issue

Post by penfold1992 »

random coding question for you masterwong... vb related.
if I was to say.... open a excel document via a button and then wanted to define that worksheet, how would i do so?
like... defining a variable that depends on what a user selects
Image

User avatar
NuclearSilo
Forum God
Posts: 8834
Joined: Mon Aug 21, 2006 12:00 pm
Quick Reply: Yes
Location: Age of Wushu

Re: coding issue

Post by NuclearSilo »

[SD]Master_Wong wrote:so i have to say addEventListener("btnanimal" & i & "()")

and that will work?

In javascript, it should, without the "()", not sure in vbscript

The correct javascript syntax is : document.getElementById('someid').addEventListener('click', 'btnanimal' & i)
Playing Age of Wushu, dota IMBA

User avatar
[SD]Master_Wong
Forum God
Posts: 9509
Joined: Wed Jan 04, 2006 8:02 pm
Quick Reply: Yes
Location: Plymouth, University

Re: coding issue

Post by [SD]Master_Wong »

NuclearSilo wrote:
[SD]Master_Wong wrote:so i have to say addEventListener("btnanimal" & i & "()")

and that will work?

In javascript, it should, without the "()", not sure in vbscript

The correct javascript syntax is : document.getElementById('someid').addEventListener('click', 'btnanimal' & i)


il have a crack but i dont think that will work as it aint auto completing the line for me besides im not overly fussed about this no more since been told i wont gain anything. im diverting my attention to see if i can dynamically create vbscript code in a loop to get rid of the btnanimal1-7_onclick() subs as they all do pretty much the same thing
MaStEr
Image
credits zelzin ^^

User avatar
NuclearSilo
Forum God
Posts: 8834
Joined: Mon Aug 21, 2006 12:00 pm
Quick Reply: Yes
Location: Age of Wushu

Re: coding issue

Post by NuclearSilo »

Use event delegation in JS to work with dynamic code.
You button click will bubble the click event to a predefined parent

First attach the parent with a click event, then inside the function you can detect from which element the event is from

Code: Select all

<head>
    <script type="text/javascript">
        function ChangeColor (event) {
            var target = event.target ? event.target : event.srcElement;
                // if the click has occurred on a button
            if (target && target.nodeName.toLowerCase () == "button") {
                target.style.color = "#ff0000";
            }
        }
    </script>
</head>
<body>
    <div onclick="ChangeColor (event)">
        Clicking on this text does not modify the text color.
        Clicking on a button modify its text color.
        <br /><br />
        <button>Change my text color to red!</button>
        <button>Change my text color to red!</button>
        <button>Change my text color to red!</button>
    </div>
</body>
Playing Age of Wushu, dota IMBA

Post Reply

Return to “Off Topic Lounge”