-1

I would like to know how to access to the php content of a html tag in jQuery. The explanation is as below:

This is my html code:

<div id="edittitle" title="Editing title">
<b><?php $id="a"; ?></b>
</div>

And this is my jQuery code:

$("#table_child_title").on("click", ".editlink", function(event){
var string2=$("#edittitle" ).children('b').contents();
alert(string2);
});

What I want is that when executing the line alert(string2); the alert window shows me what is between <b> and </b>. In other words, it shows exactly the message below:

<?php $id="a"; ?>

Is that possible in jQuery? If yes, what is wrong in my code and what is the right one?

Thanks in advance.

Chris
  • 57,622
  • 19
  • 111
  • 137

3 Answers3

0

Sure it's possible! Simply select the the <b> tag insite the #edittag div as shown below. Since php code is always executed before any jQuery script, the value of your $id variable will already be output.

alert($('#edittitle b').html());

However, you're not outputting it correctly in the code provided. Use echo to do that.

<div id="edittitle" title="Editing title">
  <b><?php echo $id="a"; ?></b>
</div>

More info about html(): http://api.jquery.com/html/

Demo

EDIT:

To echo the result AND the pre-processed php code, replace the above with:

<div id="edittitle" title="Editing title">
  <b<?php $id="a"; ?></b>
  <span>&lt;?php $id="a"; ?&gt;</span>
</div>

And in jQuery do:

alert($('#edittitle span').html());

Sadly, I don't think there is a way to do both things in one step. You have to do it separately.

Updated Demo (again)

Chris
  • 57,622
  • 19
  • 111
  • 137
  • what I'd like to obtain is : `` as the message of the alert window. Do you have any idea about how to do that? Thanks in advance. – revolution_forever Sep 29 '15 at 21:43
  • As I explained, the php code will exectute the `echo` before the `alert` is shown. So when your browers receives the html from the server, it will actually look like this: `a`, and not ``. If you (for whatever reason) still want to alert the php script, and not the value of `$id` - check @JayBlanchards answer. – Chris Sep 29 '15 at 21:47
  • @revolution_forever I understand what you want to do now. Check my answer and the updated demo. – Chris Sep 29 '15 at 22:24
0

First you must echo the PHP variable:

<div id="edittitle" title="Editing title">
<b>&lt;?php echo $id; ?&gt;</b>
</div>

The use .text() or .html() to get the contents:

var string2=$('#edittitle').children('b').text();
console.log(string2);

You should not use alert() for troubleshooting., use console.log() instead.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • what I'd like to obtain is : `` as the message of the alert window. Do you have any idea about how to do that? Thanks in advance. – revolution_forever Sep 29 '15 at 21:43
  • So - you don't want to echo the value of `$id`? You have to prevent your PHP code from processing, so you would need to use `<?php $id="a"; ?>` within your HTML. Is that what you're after? If so, my edit should work for you. – Jay Blanchard Sep 29 '15 at 21:47
  • @JayBlanchard, I'm not sure he knows the order of execution of php and js. Even though your last comment here is correct, I don't think OP knows what he's *actually* asking. – Chris Sep 29 '15 at 21:50
  • I agree @Chris, we may be tilting at windmills - such as they are. – Jay Blanchard Sep 29 '15 at 21:50
  • I don't want to prevent your PHP code from processing. I just want to display it in the alert window. – revolution_forever Sep 29 '15 at 21:54
0

Here's a working example:

<div id="edittitle" title="Editing title">
   <b><?php echo $id="a"; ?></b>
</div>

<script>
$("#table_child_title").on("click", ".editlink", function(event){
   var string2 = $("#edittitle").children('b').html();
   alert(string2);
});
</script>
  1. echo missing, that's why the variable $id is not shown
  2. Use .html() / .text() instead of .contents()

If by any chance you want to print literally: <php $id="a"; ?>

Then you should use HTML entities to print <>

<b>&lt;php $id="a"; ?&gt;</b>
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98