0

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...
    });
};
4
  • let me know if you want jsfiddle demo Commented Dec 9, 2017 at 8:27
  • @PranayRana yes, please Commented Dec 9, 2017 at 8:30
  • hi updated my answser , with jsfiddle demo : jsfiddle.net/pranayamr/uuvtx5ga, please do upvote/accept if it works for you Commented Dec 9, 2017 at 8:34
  • is that worked foryou ?? Commented Dec 9, 2017 at 8:48

2 Answers 2

1

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/

  • read more about on function here : http://api.jquery.com/on/
  • one more change in you code is always create element in html DOM with ID attribute , so i added Id for button which is btnGetInfo
Sign up to request clarification or add additional context in comments.

Comments

0
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

Comments

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.