1

Using a list of genes which I have created in a python script, I am trying to create clickable links for each gene in the list. I am using the Flask framework to accomplish this and am able to access the genes in my list with the {{genes}} variable. I am able to see this list when I view the page source, but when I try to add each gene to the body of my DOM with a javascript function I don't see anything. Does anyone know what I am doing wrong?

<!DOCTYPE html>
<html>
    <head>
        <h1>Genes<h1>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
        </script>

    </head>
    <script type ="text/javascript">
    function showGenes(){

        var name = "";
        for (gene in {{genes}}) {
            var name = document.createElement('a');
            var text = document.createTextNode(name);
            name.appendChild(text);
            document.body.appendChild(name);

        }

}
    </script>

<body onload="showGenes()">

</body>

</html>
8
  • you getting any errors in your console? Commented Oct 7, 2013 at 4:55
  • Why tagged as Python? Commented Oct 7, 2013 at 5:02
  • Why do you need javascript for creating HTML? Since you are using some templating language already, create thr HTML on the templating level. downvoted.. Commented Oct 7, 2013 at 5:06
  • It is tagged as python since I am using python's flask framework to generate the gene list. I am not sure how to create the html from the template language, or if it can be done with flask. I know it can be done with javascript, and that's why I asked this question. So how do I do it within the question that I posted? Commented Oct 7, 2013 at 5:13
  • every python web framework give you the option passing a list or a dict or whatever data to your template and use it for rendering. Commented Oct 7, 2013 at 5:17

1 Answer 1

1

In case {{genes}} is a javascript array, you need to replace your loop the following way (Just remember, it is javascript, not python, so you cannot use for - in loop that easily):

$.each({{genes}}, function(index, value){
     $('body').append($('<a></a>').text(value));
 })
Sign up to request clarification or add additional context in comments.

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.