Lombric
Lombric

Reputation: 838

Put string in href="javascript" in append

I want to put a string into a href="javascript:" when i append a li but i doesn't work at all....

 ajoutEnf("myString");
 function ajoutEnf(enf){
   $('#laliste').append('<li><a href="javascript:popEnfant('+enf+')" data-icon="edit" data-rel="popup">Enfant</a></li>');
   $('#enf').popup('close');
   $('#laliste').listview('refresh');
 }

Upvotes: 0

Views: 846

Answers (3)

skparwal
skparwal

Reputation: 1084

Do some little changes: add slashes with single quotes.

 $('#laliste').append('<li><a href="javascript:popEnfant(\''+enf+'\')" data-icon="edit" data-rel="popup">Enfant</a></li>');

Upvotes: 0

Andreas Louv
Andreas Louv

Reputation: 47127

You can bind the click event using jQuery.fn.on:

ajoutEnf("myString");
function ajoutEnf(enf){
    $('<a herf="#" data-icon="edit" data-rel="popup">Enfant</a>').on('click', function(event) {
        event.preventDefault(); // Stop the hash from changing to "" (page.html#)
        popEnfant(enf);
     }).wrap('<li/>').parent().appendTo('#laliste');
     $('#enf').popup('close');
     $('#laliste').listview('refresh');
 }

This way you dont have to string whatever enf and make sure that objects/arrays are passed on correctly.

Upvotes: 1

David Hedlund
David Hedlund

Reputation: 129832

That code will produce:

javascript:propEnfant(myString)

Thus clicking the link, the script will look for a variable named myString. You probably want to use '<li><a href="javascript:popEnfant(\''+enf+'\')" ...

Another thing to be aware of: if popEnfant is defined inside your DOMReady event listener (which I don't know if it is) it will not be globally accesible, which is a requirement for javascript:... to work.

Demo

Upvotes: 1

Related Questions