Reputation: 226
I want to create a button by using function below, example createGetInfo(120,Test,1) I want the result become a 'Test' button with 120 width but the function below fail, How can I put the parameter inside the button tag?
function createGetInfo(size,wording,filter) {
var GetInfo = $("<button class='eqGroupBtn' type='button' class='btn' style='width:size'>wording</button>");
secondLevelMenuDiv.append(GetInfo);
GetInfo.click(function(){
Do something...
});
};
Upvotes: 0
Views: 68
Reputation: 176956
for binding event with dynamically created element you need to use on
method of jquery, so your code will be as below
$( document ).ready(function() {
var size=100;
var GetInfo = $("<button class='eqGroupBtn' id='btnGetInfo' type='button' class='btn' style='width:"+size+"px;'>wording</button>");
$("body").append(GetInfo);
$( "#btnGetInfo" ).on( "click", function() {
alert( $( this ).text() );
});
});
Working jsfiddle demo : https://jsfiddle.net/pranayamr/uuvtx5ga/
btnGetInfo
Upvotes: 1
Reputation: 6639
function createGetInfo(size,wording,filter) {
var GetInfo = $("<button class='eqGroupBtn' type='button' class='btn' style='width:'+size + 'px'>wording</button>");
secondLevelMenuDiv.append(GetInfo);
GetInfo.click(function(){
Do something...
});
};
'width:'+size + 'px'
Define it like the above
Upvotes: 0