Javascript command does not work inside Buttom

Asked

Viewed 44 times

2

Good morning! I have a problem, and I’m having trouble solving it. I need a buttom to appear or not according to the PHP condition. To make the buttom appear I’m succeeding, the point is that inside it has a javascript call that doesn’t work within PHP. I don’t know what I’m doing wrong.

Follow the codes:

<header>
<script type="text/javascript">
function exibe(id) {
    if(document.getElementById(id).style.display=="none") {
        document.getElementById(id).style.display = "inline";
    }
    else {
        document.getElementById(id).style.display = "none";
    }
}
</script>
</header>

<body>
<?php  
    if (get('data_do_descredenciamento')!= ''){
        echo '<button type="button" class="btn" href="#" onclick="javascript: exibe('conteudo');">SUBSTITUTO</button>';
    }
?>
</body>

It is giving error in the content: onclick="javascript: exibe('conteudo')

Could someone help me, please?

  • 2

    What is this conteudo? is a string? You have to escape those simple quotes... like this: exibe(\'conteudo\');

2 answers

2


Your problem is because of the quotes. Here are some alternatives to solve your problem:

1° Concatenate quotes.

echo "<button type='button' class='btn' onclick="."exibe('conteudo');".">SUBSTITUTO</button>";

2º Escape simple quotes

echo '<button type="button" class="btn" href="#" onclick="javascript: exibe(\'conteudo\');">SUBSTITUTO</button>';

3rd variable with onclick

$button_onclick = "onclick="."exibe('conteudo');"."";
echo "<button type='button' class='btn' href='#' $button_onclick>SUBSTITUTO</button>";

I hope I’ve helped.

1

Replace that:

onclick="javascript: exibe('conteudo');"

That’s why:

onclick="javascript: exibe(\'conteudo\');"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.