-1

I have a ..weird problem.

I have this function JavaScript:

function example() {
    alert('a');
}

And i have this 2 inputs:

This does NOT work

<input type="image" src="img/erase.png" alt="Borrar" onclick="example();">

this WORKS

<input type="image" src="img/erase.png" alt="Borrar" onclick="alert('a');">

I know they do the SAME but in me real project i want create with PHP bucle a items and they should call JavaScript.

This example is for understan easy this problem.

Thank you ppl! :D

Solution:

In mi code in PHP i write bad the "echo" and put a lot of " and ', and when i go to write the parameters can write ' or " because they are used to write and describe the "echo" (PHP) and then i should write "&quot;".

example:

echo "<input type='image' src='img/erase.png' alt='Borrar' onClick='borrarPost(&quot;NOTICIA&quot;,&quot;".$row['Titulo']."&quot;,".$row["ID"].")'>";

the &quot; is for a parameters String, normal echo is like echo "Hi" but this who write a input need use ",' and &quot

CristianCV
  • 342
  • 1
  • 5
  • 17

1 Answers1

0

It works for me:

<!DOCTYPE html>
<html>
    <head>
        <title>test</title>
        <script>
            function example() {
                alert('a');
            }
        </script>
    </head>
    <body>

        <div>
            <input type="image" src="img/erase.png" onclick="example()">
        </div>

    </body>
</html>

Note that it only works if you have the example function in the root scope of the javascript. For example, this wouldn't work:

window.onload = function() {
    function example() {
        alert('jo');
    }
}
gitaarik
  • 42,736
  • 12
  • 98
  • 105
  • i think the problem is because is a input type img...and you no use input type image – CristianCV Apr 07 '14 at 11:55
  • No, that's not the problem, check my updated answer. – gitaarik Apr 07 '14 at 11:57
  • Try to find the differences. Try to remove code to make it look like mine and test each time you've changed something, when it starts working, you know where the problem is. – gitaarik Apr 07 '14 at 12:01
  • I just copy you code in one test HTML and works, and i go to the document when i need and no works...that's is sooo weird – CristianCV Apr 07 '14 at 12:03
  • 1
    @CristianCV Never say "I found the problem" and not explain what you did. That just means that the next person who's trying to find an answer will end up with a "nevermind I fixd the problem" answer, and not an actual answer. – h2ooooooo Apr 07 '14 at 12:30