function call concatination

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    function call concatination

    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)Lwd K62pz...YfA%3D% 3D (line 2)
    [Break on this error] close_msg_divs( error);"

    thanks for any help in advance here is the source

    [code=javascript]
    function msg_divs(hldr,x ,y,w,h,data){// description of the arguments below
    alert(hldr);
    holder=document .getElementById (hldr); //wrap around div
    holder.innerHTM L="";
    holder.style.vi sibility='visib le';
    holder.style.di splay='block'; // the div gets styled
    holder.style.po sition='absolut e';
    holder.style.le ft=x;
    holder.style.to p=y;
    holder.style.wi dth=w;
    holder.innerHTM L=data;
    holder.innerHTM L+="<input name='close' id='close' type='button' value='lose' class='button' onclick='close_ msg_divs("+hldr +")' />";
    alert(holder.in nerHTML);
    }
    function close_msg_divs( obj){
    alert(obj);
    document.getEle mentById(obj).i nnerHTML='';
    holder.style.vi sibility='hidde n';
    }
    [/code]
    [code=html]
    <div id="error" class='error'></div>
    [/code]
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    please try the following and note the changes with the setting of quotes:

    [CODE=javascript]holder.innerHTM L += '<input name="close" id="close" type="button" '
    + 'value="lose" class="button" onclick="close_ msg_divs(\''
    + hldr
    + '\')" />';[/CODE]
    kind regards

    Comment

    • omerbutt
      Contributor
      • Nov 2006
      • 638

      #3
      Originally posted by gits
      please try the following and note the changes with the setting of quotes:

      [CODE=javascript]holder.innerHTM L += '<input name="close" id="close" type="button" '
      + 'value="lose" class="button" onclick="close_ msg_divs(\''
      + hldr
      + '\')" />';[/CODE]
      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.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        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:

        [CODE=javascript]onclick="close_ msg_divs('id_st ring');"
        [/CODE]
        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

        Comment

        • omerbutt
          Contributor
          • Nov 2006
          • 638

          #5
          Originally posted by gits
          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:

          [CODE=javascript]onclick="close_ msg_divs('id_st ring');"
          [/CODE]
          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
          Code:
          document.getElementById("input_tag_id").onclick="close_msg_div('id_string')";
          that is wot confused me i tried the same way with the innerHTML
          thanks to Gits :)
          regards,
          Omer Aslam

          Comment

          Working...