0

i am creating a dynamic button in javascript and calling a function on its click,but I am not able to pass string parameter to it,what shoud be the proper code. I am doing this.

  document.getElementById(divfield).innerHTML +="<input type='button' value='-'class='remove_this"+i+"' onclick='  removed("+i+",'"+s+"')  '>";

here i is a integer,and s is a variable holding string.how should i pass string variable.

3
  • Could you create jsfiddle page for us? Commented Feb 28, 2014 at 11:34
  • What did you actually get from this code? Commented Feb 28, 2014 at 11:35
  • whatever m passing as string,it is giving as variable... Commented Feb 28, 2014 at 11:46

2 Answers 2

2

I guess you want something like this:

var div = document.getElementById('divfield');

var input = document.createElement('input');
input.type = 'button';
input.value = '-';
input.setAttribute('class', 'remove_this' + i);
input.onclick = function () {
    removed(i,s)
}

div.appendChild(input);

jsfiddle: demo

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

2 Comments

yes somewhat but,i want to pass a string onclick of the dynamic button
Well thanks everyone, I m able to achieve this,so sharing the piece of code,may be anyone else can get help. document.getElementById(divfield).innerHTML +="<input type='button' value='-'class='remove_this"+i+"' onclick=' removed("+i+",\""+s+"\") '>";
0

you have missed some quotation mark on your code, if you try this:

innerHTML +="<input ..... onclick=' removed(" + i + ",\" " + s + "\ ") '>";

there will not any problem...

by your code, input element will be created like this: (notice to onclick attribute):

<input type="button" value="-" class="remove_this1" onclick="  removed(1," ss') '>

if: (i = 1) and (s = "ss") for example

1 Comment

thanks,this was the code,it worked for me,but I tried it by self before you posted.But thanks for answering.

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.