954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

passing javascript variable inside innerhtml

function func1(str)
{

 document.getElementById("txtHint").innerHTML='<Br>Graph type: <select id="sel"  name="sel" onchange="func2(str)"><option value="1">1</option><option value="2"> 2</option></select>';

}

function func2(x)
{

some codes....

}


In Above coding,

i want to pass the "str" variable to "func2" in onchange .

but above code is not working.

Can anyone Please help me..?

DILO3D
Newbie Poster
21 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

3D,

//This is the bad way
function func1(str){
  document.getElementById("txtHint").innerHTML = 'Graph type: <select id="sel" name="sel" onchange="func2(\'' + str + '\')"><option value="1">1</option><option value="2"> 2</option></select>';
}

//This is preferred
function func1(str){
  document.getElementById("txtHint").innerHTML = 'Graph type: <select id="sel" name="sel"><option value="1">1</option><option value="2"> 2</option></select>';
  document.getElementById("sel").onchange = function(){
    func2(str);
  };
}

For a number of reasons, event handlers are better attached in javascript than in HTML.Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

Thankyou verymuch.i solved my problem.your answer was very helpful to me... :)

DILO3D
Newbie Poster
21 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: