views:

31

answers:

1

Hi,

I'm dynamically generating forms with hidden input in a webpage using django templates, which should be submitted when clicking on some associated text label. The problem is that I just can't get it to work.

{% for tag in tags %}
<form name="remove_{{ tag }}" id="{{ tag }}" action="/filter" method="post" accept-charset="utf-8">
    <input type="hidden" name="remove_tag" value ="{{ tag }}" />
</form>

<a href="javascript: remove_tag({{ tag }})">{{ tag }}</a>
{% endfor %}

<script type="text/javascript">
    function remove_tag(tag) {  
        document.getElementById(tag).submit();
        return false;
    }
</script>

This code generates the following javascript error: Uncaught TypeError: Cannot call method 'submit' of null

I've also tried submitting the form using

document.forms[tag].submit();

(changing the form name to tag), but receive pretty much the same error but with 'undefined' instead of null.

In the second example, it looks like the javascript function tries to interpret 'tag' as an integer. If I do use an integer it works alright. I could use the forloop.counter used to generate the forms in the first place, but that is kind of ugly and makes the code harder to maintain.

Is there any other, functioning, way of calling submit() on the forms?

+1  A: 

Quotes:

<a href="javascript: remove_tag('{{ tag }}')">{{ tag }}</a>

The "tag" value has to be properly quoted for the Javascript snippet to be syntactically correct.

Pointy
Too easy ;) Thanks!
aspartame