Yahreen
Yahreen

Reputation: 179

Insert a character entity on .click() with jQuery

I'd like to insert the character entity ✓ (✓) at the end of a list item after an href has been clicked:

<a href="" id="answer1">Click here to answer</a>

<ul>
<li>Answer 1</li>
</ul>

$("#answer1").click(function(){
// Add &#10003; to the end of the list item, such as: "<li>Answer 1 &#10003;</li>"
});

Any help is much appreciated! Thanks!

Upvotes: 2

Views: 930

Answers (2)

Gaurav
Gaurav

Reputation: 28755

I don't know how much li elements you have, but for this html as you shown

$("#answer1").click(function(){
    $('ul li').append('&#10003;');
});

Upvotes: 0

Jason Jong
Jason Jong

Reputation: 4330

You can use the following code

$("#answer1").click(function(){
   $(this).html() += $(this).html() + '&#10003'; // just format the Html anyway you want
});

Edit

I mis-read your question.

for your UL ie... its something like

<ul>
   <li>Answer 1</li>
   <li>Answer 2</li>
   <li>Answer 3</li>
</ul>

then your Html to show the answers - perhaps store the answers index:

<a href="" class="anwers" data-answer="0">Click here to answer</a>

in your javascript

$("a.answers").click(function(){
   // get the answer index
   var i = $(this).attr("data-answer");

   var answer = $("ul > li:eq(" + i + ")");
   answer.html( answer.html() + '&#10003' );
 });

Upvotes: 1

Related Questions