By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

function call concatination

omerbutt
100+
P: 638
hi there i making a div based alert and for that i want to make a button in side a div that could close that div too i know that is not a big case but some how i am making some kind of mistake the function msg_divs() is called if the form validation is not successful and throws th errors inside the div and after that it makes a button and that calls a function close_msg_div() that clears the innerHTML of he "error" div but here is the error when i try to clear that div i think that i am not concatinating the function in the "onclick property" of the close button ,some times it gives
"syntax error close_msg_divs("
and some times
"error is not defined
onclick(click clientX=726, clientY=177)LwdK62pz...YfA%3D%3D (line 2)
[Break on this error] close_msg_divs(error);"

thanks for any help in advance here is the source

Expand|Select|Wrap|Line Numbers
  1. function msg_divs(hldr,x,y,w,h,data){// description of the arguments below
  2.     alert(hldr);
  3.    holder=document.getElementById(hldr); //wrap around div
  4.    holder.innerHTML="";
  5.    holder.style.visibility='visible';
  6.    holder.style.display='block';  // the div gets styled
  7.    holder.style.position='absolute';
  8.    holder.style.left=x;
  9.    holder.style.top=y;
  10.    holder.style.width=w;
  11.    holder.innerHTML=data;
  12.    holder.innerHTML+="<input name='close' id='close' type='button' value='lose' class='button' onclick='close_msg_divs("+hldr+")' />";
  13.    alert(holder.innerHTML);
  14. }
  15.     function close_msg_divs(obj){
  16.         alert(obj);
  17.         document.getElementById(obj).innerHTML='';
  18.         holder.style.visibility='hidden';
  19.     }
  20.  
Expand|Select|Wrap|Line Numbers
  1. <div id="error" class='error'></div>
  2.  
Jul 23 '08 #1
Share this Question
Share on Google+
4 Replies


gits
Expert Mod 5K+
P: 5,343
please try the following and note the changes with the setting of quotes:

Expand|Select|Wrap|Line Numbers
  1. holder.innerHTML += '<input name="close" id="close" type="button" ' 
  2.                     + 'value="lose" class="button" onclick="close_msg_divs(\''
  3.                     + hldr 
  4.                     + '\')" />';
kind regards
Jul 23 '08 #2

omerbutt
100+
P: 638
please try the following and note the changes with the setting of quotes:

Expand|Select|Wrap|Line Numbers
  1. holder.innerHTML += '<input name="close" id="close" type="button" ' 
  2.                     + 'value="lose" class="button" onclick="close_msg_divs(\''
  3.                     + hldr 
  4.                     + '\')" />';
kind regards
BINGO BUDDY :) can you tell me the logic for using backslashes i have worked a lot on javascript but this is the first time i have come accross this problem
thanks anyways ,
ragrds,
Omer Aslam.
Jul 24 '08 #3

gits
Expert Mod 5K+
P: 5,343
with the backslash you 'escape' special characters that you want to use with strings ... so the interpreter handles them as a part of the string and not as an instruction ... this is often needed in our current case ... since you have to pass the id as a string to the function you have to add quotes to the onclick-function call and! around the id that is passed to it ... but for later evaluation of the instruction you need different ones:

Expand|Select|Wrap|Line Numbers
  1. onclick="close_msg_divs('id_string');"
  2.  
since you used innerHTML you have to construct a complete and syntactically correct string and as you see for the part above we need single and! double quotes ... so you have to escape the ones that would break the entire string since we have to include them to the string itself ...

kind regards
Jul 24 '08 #4

omerbutt
100+
P: 638
with the backslash you 'escape' special characters that you want to use with strings ... so the interpreter handles them as a part of the string and not as an instruction ... this is often needed in our current case ... since you have to pass the id as a string to the function you have to add quotes to the onclick-function call and! around the id that is passed to it ... but for later evaluation of the instruction you need different ones:

Expand|Select|Wrap|Line Numbers
  1. onclick="close_msg_divs('id_string');"
  2.  
since you used innerHTML you have to construct a complete and syntactically correct string and as you see for the part above we need single and! double quotes ... so you have to escape the ones that would break the entire string since we have to include them to the string itself ...

kind regards
:0 thanks alots Gits that some how cleared the diff between innerHTML and using simple javascript method like
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("input_tag_id").onclick="close_msg_div('id_string')";
  2.  
that is wot confused me i tried the same way with the innerHTML
thanks to Gits :)
regards,
Omer Aslam
Jul 24 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.