2
parameters=eval([[\"April\",[[\"medical\",\"1\"],[\"financial\",\"4\"],[\"burial\",\"1\"]]],[\"May\",[[\"medical\",\"2\"],[\"financial\",\"6\"],[\"burial\",\"6\"]]]]);   

<input type="submit" value="Pie Chart" onClick="showChart('<?php echo $title;?>',parameters,'#container','chart','Pie Chart')"/>

this works fine when I'm just including this into my html code. but I want to append this into a specific div using javascript or jquery. like this.

<script>
    parameter=eval("[[\"April\",[[\"medical\",\"1\"],[\"financial\",\"4\"],[\"burial\",\"1\"]]],[\"May\",[[\"medical\",\"2\"],[\"financial\",\"6\"],[\"burial\",\"6\"]]]]");

    myButton="<input type="submit" value="Pie Chart" onClick="showChart('<?php echo $title;?>',parameters,'#container','chart','Pie Chart')"/>";
    $('#content').append(myButton);
</script>

My problem is, it does not perform the function whenever I click the button. Maybe because of the variable parameter that I passed and my question is how can I do this correctly? And I'm avoiding using ajax cause it will affect a big portion of my codes.

3
  • never use - eval - stackoverflow.com/questions/86513/… Commented Jun 24, 2014 at 5:52
  • i used eval to convert the returned json-encode into a javascript object. it worked in hmtl way, so in script i should not use it? Commented Jun 24, 2014 at 6:00
  • yes, you should not instead use JSON.parse Commented Jun 24, 2014 at 6:04

2 Answers 2

1

Try this : use escape characters and add parameter variable using plus operator

<script>
    parameter=eval("[[\"April\",[[\"medical\",\"1\"],[\"financial\",\"4\"],[\"burial\",\"1\"]]],[\"May\",[[\"medical\",\"2\"],[\"financial\",\"6\"],[\"burial\",\"6\"]]]]");

    myButton="<input type=\"submit\" value=\"Pie Chart\" onClick=\"showChart('<?php echo $title;?>',"+parameters+",'#container','chart','Pie Chart')\"/>";
    $('#content').append(myButton);
</script>
Sign up to request clarification or add additional context in comments.

Comments

0
jQuery('#yourbutton').on('click', function() {           
   });  // your on click event
  //to add content to innerhtml of div:
  jQuery('#divelementorother').html(jQuery('#divelementorother').html() +   YourDataToInside);

First, you see the .on function of jQuery to get an event handler on click to execute function. You can append that as JavaScript to the html of div, so you doesn`t have to use the button itselfs onclick

Second, to extend the innerhtml or html of an elemnt like , you've to use .html(data) . Or, i see above my post: you can use append.

Greetings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.