prk
prk

Reputation: 3809

Too many quote marks - How would I echo this without closing them up

Basically I want to have my spoiler thingy inside an echo, but can't get it to work due to the quotes marks confusing me, hah.

echo "<input class='spoilerbutton' type='button' value='Register' onclick='this.value=this.value=='Register'?'Cancel':'Register';'><div class='spoiler'><div>woooohoo hide this text</div></div>";

As you can see by the 'register'?'cancel' part, there are quotes that gets closed by one another.

How could I fix this the most simple way? I'm getting too confused, lol.

Upvotes: 0

Views: 215

Answers (2)

Philip Bennison
Philip Bennison

Reputation: 527

You can escape the quotation marks like so

echo "<input class='spoilerbutton' type='button' value='Register' onclick='this.value=this.value==\"Register\"?\"Cancel\":\"Register\";'><div class='spoiler'><div>woooohoo hide this text</div></div>";

Upvotes: 2

Quentin
Quentin

Reputation: 943564

Don't put HTML inside PHP strings if you can help it. Turn it inside out.

if (somecondition) {
    ?>
    <input 
        class='spoilerbutton' 
        type='button' 
        value='Register' 
        onclick="this.value=this.value=='Register'?'Cancel':'Register';">
    <div class='spoiler'>
        <div>woooohoo hide this text</div>
    </div>
    <?php
} else {

}

For that matter, don't put JavaScript inside HTML attributes if you can help it.

<input 
    class='spoilerbutton' 
    type='button' 
    value='Register'>
<script>
     var input = document.querySelector('input.spoilerbutton')l
     input.addEventListener('click', toggleValue);
     function toggleValue(evt) {
         this.value=this.value=='Register'?'Cancel':'Register';
     }
</script> 

Upvotes: 3

Related Questions