How to insert $ variable in jQuery

I work in Drupal using jQuery. How to insert php $ variable in tag.

$(document).ready(function(){
    $("#comment-delete-<?php print $variable ?>").click(function(){
        $("div.comment-<?php print $variable ?> span.body").replaceWith("new text");
    });
})

or

$(document).ready(function(){
    $("#comment-delete-". $variable).click(function(){
        $("div.comment-". $variable ." span.body").replaceWith("new text");
    });
})

A few things to clean. I work in Drupal, so the full code is as follows:

<?php
drupal_add_js (
    '$(document).ready(function(){
        $("#comment-delete-"' print $comment->cid; ').click(function(){
            $("div.comment-"' print $comment->cid; '" span.body").replaceWith("<span style=\'color: grey;\'>Please wait...</span>");
        });
    })',
'inline');
?>

but it still does not work.

Update: I tried the following, but it still does not work

<?php
$testing = "42";
drupal_add_js (
    '$(document).ready(function(){
        $("#comment-delete-"'. $testing .').click(function(){
            $("div.comment-"'. $testing .'" span.body").replaceWith("<span style=\'color: grey;\'>Please wait...</span>");
        });
    })',
'inline');
?>

If I use the number "42" instead of a variable, it works, but not when using a variable ... weird.

0
source share
3 answers

Based on your comment:

<?php 
    drupal_add_js ('
        $(document).ready (function() { 
            $("#comment-delete-' . $variable . '").click (function() { 
                $("div.comment-' . $variable . ' span.body").replaceWith ("new text"); 
            }); 
        })
    ','inline');
?>

You must concatenate $variableinstead of print-in it

+3
source
$(document).ready(function(){
    $("#comment-delete-<?php print $variable ?>").click(function(){
        $("div.comment-<?php print $variable ?> span.body").replaceWith("new text");
    });
})

PHP , , . , , , ... .


.

PHP HTML-
PHP <?php ?> :

$(document).ready(function(){
    $("#comment-delete-<?php print $variable ?>").click(function(){
        $("div.comment-<?php print $variable ?> span.body").replaceWith("new text");
    });
})

:

$(document).ready(function(){
    $("#comment-delete-mytag").click(function(){
        $("div.comment-mytag span.body").replaceWith("new text");
    });
})

Javascript:

$(document).ready(function(){
    $("#comment-delete-mytag").click(function(){
        $("div.comment-mytag span.body").replaceWith("new text");
    });
})

, PHP - . , , PHP, Javascript Code. PHP Javascript. Javascript Syntax, . AKA <?php ?>, , , , Javascript.


, , , , :

drupal_add_js (
    '$(document).ready(function(){
        $("#comment-delete-'. $variable . ').click(function(){
            $("div.comment-'. $variable . ' span.body").replaceWith("<span   style=\'color: grey;\'>Please wait...</span>");
        });
    })',
'inline');
+6

[] Ooops, , , , , .

0

Source: https://habr.com/ru/post/1727052/


All Articles