Problem while dynamically passing params to javascript functions.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • haijdp
    New Member
    • Jun 2007
    • 10

    Problem while dynamically passing params to javascript functions.

    Hi,

    I am using the following javascript method inside a js file.

    function createDivRow(la bel,text) {

    var formedTd = '<tr><td>' + label + '</td><td> <a href="#" onclick="javasc ript:eventHandl erFunction("'+t ext+'")">'+text +'</a></td></tr>';
    return formedTd;
    }

    But it's throwing syntax error.
    I have tried the following way also.

    function createDivRow(la bel,text) {

    var formedTd = '<tr><td>' + label + '</td><td> <a href="#" onclick="javasc ript:eventHandl erFunction('+te xt+')">'+text+' </a></td></tr>';
    return formedTd;
    }

    this version throwing ')' expected and also, the value of text which i am passing dynamically is undefined.

    I am not able to call the eventHandlerFun ction() by passing the dynamic value of the 'text' .

    I am using IE6. can any one help me....
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    try this:

    [CODE=javascript]
    var formedTd = '<tr><td>'
    + label
    + '</td><td> <a href="#" onclick="javasc ript:eventHandl erFunction("'
    + text
    + '")">'
    + text
    + '</a></td></tr>';
    [/CODE]

    the problem should be your linebreak before 'onclick'

    kind regards ...

    Comment

    • haijdp
      New Member
      • Jun 2007
      • 10

      #3
      Originally posted by gits
      hi ...

      try this:

      [CODE=javascript]
      var formedTd = '<tr><td>'
      + label
      + '</td><td> <a href="#" onclick="javasc ript:eventHandl erFunction("'
      + text
      + '")">'
      + text
      + '</a></td></tr>';
      [/CODE]

      the problem should be your linebreak before 'onclick'

      kind regards ...
      Hi...
      Thanks for your reply. I am still getting the syntax error...:(

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        ... when trying this in FF & IE it works for me ...

        [HTML]<body>
        <script>
        var label = 'test';
        var text = 'test1';

        var formedTd = '<tr><td>'
        + label
        + '</td><td> <a href="#" onclick="javasc ript:eventHandl erFunction("'
        + text
        + '")">'
        + text
        + '</a></td></tr>';

        alert(formedTd) ;
        </script>
        </body>
        [/HTML]

        please post your code that is not working ...

        kind regards ...

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          ahhhhhhhhhh ... got your problem ... you put the formedTd-string in an HTML-page and you want the function called ... ;) of course ...

          you must escape the quotes and single quotes of course ... sorry that i forgot it ... use the following:

          [CODE=javascript]
          var formedTd = '<tr><td>'
          + label
          + '</td><td> <a href="#" onclick="javasc ript:eventHandl erFunction(\''
          + text
          + '\')">'
          + text
          + '</a></td></tr>';
          [/CODE]

          but note ... that assumes that text is a string passed to your eventHandlerFun ction ... otherwise you should delete the both escaped quotes

          kind regards

          Comment

          • haijdp
            New Member
            • Jun 2007
            • 10

            #6
            Originally posted by gits
            ahhhhhhhhhh ... got your problem ... you put the formedTd-string in an HTML-page and you want the function called ... ;) of course ...

            you must escape the quotes and single quotes of course ... sorry that i forgot it ... use the following:

            [CODE=javascript]
            var formedTd = '<tr><td>'
            + label
            + '</td><td> <a href="#" onclick="javasc ript:eventHandl erFunction(\''
            + text
            + '\')">'
            + text
            + '</a></td></tr>';
            [/CODE]

            but note ... that assumes that text is a string passed to your eventHandlerFun ction ... otherwise you should delete the both escaped quotes

            kind regards
            yeah...,with the single quote escapes, it's working now...
            Thanks a lot for your help.I tried different ways of calling that function with the dynamic value of 'text'.But nothing helped me. I am really thankful to you...:)

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              glad to help you ... come back when you have more questions ... ;)

              kind regards ...

              Comment

              Working...