gerardet46
gerardet46

Reputation: 76

Change element's innertext with php

I want to change a innerText of a element using php (I also use jquery); and I've tried this:

<?php
    $variable = "Hello";
    echo '<script>$('#element').text($variable);</script>'
?>

But it didn't work. I've also tried with:

document.getElementsById('element').innerText = $variable;

Upvotes: 1

Views: 2201

Answers (2)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You have a quotes problem as @Robiseb mentioned in the above comment, use :

echo "<script>$('#element').text('" . $variable . "');</script>"
//Or
echo "document.getElementsById('element').textContent = '" . $variable . "';"

Hope this helps.

Upvotes: 2

Lamine K
Lamine K

Reputation: 76

Use

 document.getElementById('element').innerHTML = $variable;

or

$('#element').html($variable);

Upvotes: 1

Related Questions