1

What i would like to do is click the button and have it up date the background of another div. The buttons are generated with php from imgs in a file.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <title></title>
    <style>
        button{width: 100%; }
    </style>
</head>
<body>
    <div id="Navi" style="width: 150px; height: 640px;background: green;">
        <?php
            foreach (glob("*.png") as $filename) {
                echo "<button id='$filename' onclick='change(event);'>".substr($filename, 0, -4)."</button></br>";
           }
        ?>
    </div>
    <div id="ImgBig" style="height: 640px; width: 640px;margin-left: 150px; margin-top: -640px; background: blue;"></div>
    <script>
        function change(event){
            $("#ImgBig").css("background-image", "url("+event.target.id+")"); 
        }
    </script>
</body>

2
  • dose not work and i have no idea why Commented Jan 23, 2016 at 15:54
  • could you please define 'does not work' ? you have a few possible problems. - Image paths not specified correctly - background is not changed etc. Also could you please add a real array instead of glob("*.png") ? Commented Jan 23, 2016 at 16:00

1 Answer 1

1

Please try to change event to this in your php target so that your javascript function is applied to the current item:

<?php
            foreach (glob("*.png") as $filename) {
                echo "<button id='$filename' onclick='change(this);'>".substr($filename, 0, -4)."</button></br>";
           }
        ?>

then in your javascript change function, just remove target:

function change(event){
            $("#ImgBig").css("background-image", "url("+event.id+")"); 
        }
Sign up to request clarification or add additional context in comments.

3 Comments

Well, that was one of the issue now resolved. What does your debugging tools indicates to you?
the debugging tool don't give say anything is wrong
well, as for my answer, it is the main problem (just remove target), please to check this: jsfiddle.net/vhdhyjjb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.