V.David V.David - 1 month ago 8
HTML Question

Create a hyperlink automatically asp.net

I want to create automatically some hyperlinks for my webpage soo i can make the pagination on my comment section with those. My comment section only allows 5 commentaries at the time storing them on a separate div called #comentario+(and the number of it), if you want to see more you have to click on the hyperlinks and it will show the div with those commentaries. But the thing is, when the hyperlinks are created the function onclick messes up with the html and doesnt appear like it should.


  • Rs is the number of the commentaries (it works fine)

  • My problem is the value that aux is holding
    "onclick='$('#comentario"+contador3+"').show()'>"+contador3+"</a>";


    for (int tst = 0; tst < rs; tst++)
    {
    contador3++;
    aux="<a onclick='$('#comentario" + contador3 +"').show()'>"+ contador3+"</a>";
    //vermaiscomentarios.Text += "<li style='text-align: center;float: left;width: 3%; margin:0;padding:0;'><span onclick='$('#comentario" + contador3 + "').show();'>" + contador3 + "</span></li>";
    vermaiscomentarios.Text += "<li style='text-align: center;float: left;width: 3%; margin:0;padding:0;'>"+aux+"</li>";
    }



On html it appears
<a onclick="$(" #comentario2').show()'="">2</a>


Any ideas soo it can work just fine?

Answer

You are breaking the strings by using their delimiter inside them. You can escape such characters to use them inside strings.

This is wrong (note the second ' ends the value held by onclick):

aux="<a onclick='$('#comentario" + contador3 +"').show()'>"+ contador3+"</a>";

This is right:

aux = "<a onclick=\"$('#comentario" + contador3 + "').show()\">" + contador3 + "</a>";