Connecting Tech Pros Worldwide Forums | Help | Site Map

Calling Javascript function within innerHTML

Member
 
Join Date: May 2007
Posts: 34
#1: Dec 7 '07
Is there anyway to call a javascript function within an innerHTML call?

example:

Expand|Select|Wrap|Line Numbers
  1. var someelement =  document.createElement("input"); 
  2. var someelement2  =  document.createElement("input"); 
  3.  
  4. function test(str) { var str2 = test.substring(0,4); 
  5.                            return str2; }
  6.  
  7. someelement.ondblclick=function(){ 
  8.        document.getElementById('someelement2').innerHTML='<input id=\'blah\' value=\'test(this.value)\' >';
  9. }
  10.  
  11. \\I think this works if the quotes are escaped properly, but i haven't been \\successful in doing it.
  12.  
  13. someelement.ondblclick=function(){ 
  14.        document.getElementById('someelement2').innerHTML='<input id=\'blah\' value=\' '+test(+' this.value ' +) ' +\' >';
  15. }
  16.  
  17.  
any help will be much appreciated.



Ferris's Avatar
Member
 
Join Date: Oct 2007
Location: Shanghai
Posts: 102
#2: Dec 8 '07

re: Calling Javascript function within innerHTML


Hi:

1st: input element can't use innerHTML.Only div element can use innerHTML.

2st: you create 2 input elements by using javascript,but you didn't add it into document.

I can't understand what you want to do,so tell me what you want,and I can post the code you want.
Member
 
Join Date: May 2007
Posts: 34
#3: Dec 11 '07

re: Calling Javascript function within innerHTML


Thanks for the reply.

I got around this problem by using an invisible input field in the innerHTML

Expand|Select|Wrap|Line Numbers
  1.  
  2. document.getElementById(something).innerHTML = data + '<input name=datafield id=datafield value=data class=nosee>';
  3.  
This seems to work for me, so i decided to just go with this method.
Reply