Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
163
rated 0 times [  164] [ 1]  / answers: 1 / hits: 22669  / 11 Years ago, wed, october 23, 2013, 12:00:00

This is my code for html



 <div id=content></div> 


Then I append an inpunt to #content:



 $( document ).ready(function() {
// Handler for .ready() called.
var parameter = <p>Hola</p>;
$(#content).append('<div><p>click the button</p>'+
'<input type=submit name=submit_answers value=Submit onclick=getValue(); >'+
'<input type=submit name=submit_answers value=Submit onclick='+getValue2(parameter)+' >'+
'</div>');
});

function getValue2(parameter){
alert(parameter);
}

function getValue(){
alert(Hola);
}


The first input works very well, but the second input dosen´t work after document is ready. What´s the better way to declare a function in this case?


More From » jquery

 Answers
12

You could do this:



onclick=getValue2(' + parameter + ')


But something like this would be better:



var $div = $('<div><p>click the botton</p></div>');
var $button = $('<input type=submit name=submit_answers value=Submit>')
.data('parameter', parameter)
.click(function () {
getValue2($(this).data('parameter'));
}).appendTo($div);

$(#content).append($div);

[#74780] Tuesday, October 22, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
seth

Total Points: 307
Total Questions: 114
Total Answers: 96

Location: Kenya
Member since Mon, Jun 14, 2021
3 Years ago
;