JQuery: onclick instead of href = javascript ::

Yes, I was told a couple of times using onclick

$('#linkId').click(function() 

instead

<a href='javascript:showComments($displayWall[id]);'>

But then how can I save a variable in a function, how can I do with this above?

I mean, the link is this: <a id="linkId">test</a>where can I save a variable?

+3
source share
3 answers

Update:

It appears to be $displayWalla global JS variable ( <a href='javascript:showComments($displayWall[id]);'>it wonโ€™t work otherwise ). Then this should work too:

$('#linkId').click(function() {
    showComments($displayWall[id]);
});

Now I understand that the following assumption is far, but nonetheless:

Assuming that it $displayWallis a server-side variable, that is, the page is pre-processed.

You can, for example, set the attribute of a reflink to the value of a variable:

<a id="linkId" ref="$displayWall[id]">test</a>

:

$('#linkId').click(function() {
    showCmments($(this).attr('ref'));
    //...
});

, :

$('#linkId').click(function() {
    var value = '$displayWall[id]';
    //...
});
+5

<a id="linkId" name="SOME_DATA">test</a>. :

$('#linkId').click(function(){
    alert($(this).attr('name'));
});
0

You can use other attributes to store the dynamic value (e.g. id, name, rel, class, etc.). One example might be:> PHP / HTML:

echo '<a class="showComments" id="c',$displayWall['id'],'" href="#">Show comments</a>';

JQuery

$(function(){
  $("a.showComments").click(function(){
    var id = $(this).attr("id").substr(1);
    // ...
    return false;
  });
});

If your variable is the same for all elements, you can define it as a javascript variable.

var myVar = "<?=$myVar;?>";
0
source

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