1

so i have some html elements generated from PHP since most contents are fetched from the database

i.e.

<?php
    ...
    while($row = $sqlQry->fetch_object()) {
        echo "<li class = 'lstOption' id = 'opt$row->name' data-value = '$row->total_count'>
                  <h3>$row->given_reference<h3>
              </li>";
    }
    ...
?>

and this is a sample structure based on my javascript

<script>
    $("ul li").click(function(){
        alert($(this).data('value'));
    });
</script>

but if i inject an onClick= attribute while inside the echo. the script executed properly. what i need is for the script to work with the echo-ed html elements from php.

5 Answers 5

3

Try using variable interpolation syntax, i.e. wrap your variables around curly braces

"{$var}" take note that you have to use double quotes("") for this

"<li class = 'lstOption' id = 'opt{$row->name}' data-value = '{$row->total_count}'>
              <h3>{$row->given_reference}<h3>
          </li>"
Sign up to request clarification or add additional context in comments.

2 Comments

uhm. what are they for ?
basically telling a php string that you are injecting value of a variable to it, without using concatenation, it also works on arrays i.e. "lorem {$arr['ipsum'}"
2

Just put your script inside document ready as below. That will fix the issue

 $(document).ready(function(){
   $("ul li").click(function(){
      alert($(this).data('value'));
   });
 });

2 Comments

the script is already inside $(document).ready(function(){}); sir. still not fixed
Are you seeing this generated html in firebug ?
2

You should try the code binding with the $(document).ready() method. This is because once your all DOM elements are ready then after code bind with document ready will appear. so do the following code.

<?php
    ...
    while($row = $sqlQry->fetch_object()) { ?>
        <li class = 'lstOption' id = 'opt<?php echo $row->name?>' data-value = '<?php echo $row->total_count?>'>
                  <h3><?php $row->given_reference?><h3>
              </li>";
    }
<?php    ...
?>


<script>
$(document).ready(function(){
 $("ul li").click(function(){
        alert($(this).data('value'));
    });
});
</script>

or write as follows

<script>
    $(document).ready(function(){
     $("ul li").on("click",function(){
            alert($(this).data('value'));
        });
    });
    </script>

2 Comments

the script is already within $(document).ready(function(){}); sir
So can you please let me know the full HTML code of yours? So I can able to fix it
1

You can try to select by id and use the on() method:

$(document).on('click', '[id^="opt"]', function () {});

Works only if you don't have anything else with id starting with "opt". In my opinion id selectors are always stronger than any other selector. The on() method is also preferable as it is also valid for dynamically generated elements (in case of adding content via ajax for example)

Also if you want to print the actual value and not the formula in your data-value, your quotes are wrong, here is the correct version :

while($row = $sqlQry->fetch_object()) {
    echo "<li class = 'lstOption' id = 'opt".$row->name."' data-value = '".$row->total_count."'>
              <h3>".$row->given_reference."<h3>
          </li>";
}

1 Comment

@TheQuestioner I think your issue comes from your quotes and that you do not concatenate the php parts. Read my edit
1

try like

$(document).ready(function(){
   $("ul li").on('click', function(){
      console.log($(this).data('value'));
   });
 });

Since your html elements are generating dynamically you have to use on()

2 Comments

according to jquery documentation as of 1.7 it was deprecated and as of 1.9 it was removed. i am using 2.1.4 atm
@TheQuestioner give a try to on()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.