0

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');
 }

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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>');

2 Comments

This will fail horribly if end == '">whoops</a><a href="'
oops. sorry my mistake. it should be with single quote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.