madmed
madmed

Reputation: 636

Twitter bootstrap collapsable doesn't work on content loaded by jquery .html()

Edit: solved by linking the correct version of jquery I have a web application built with django. I have Search page that uses jquery to show the results without refreshing the page. I want to use collapsable on the search results but it doesn't work. Here is the code, /query renders results.html. Search.html:

{% extends "base.html" %}
{% block nav3 %}class="active"{% endblock %}
{% block container %}
<script type="text/javascript">
$(document).ready(function() {
    $("#exec").click(function() {
            var dataString = 'arg1='+ $("input#arg1").val() + '&rel=' + $("input#rel").val() + '&arg2=' + $("input#arg2").val();  
            //alert (dataString);return false;  
            $.ajax({  
                type: "GET",  
                url: "/query",  
                data: dataString,  
                success: function(data) {  
                    $(".res").html(data)
                }  
            });  
            return false; 
  });
});

</script>
<form class="form-horizontal">
    <fieldset>
          <legend>Search Relations</legend>
            <div class="control-group">
                <label class="control-label">   Argument1 </label>
                <div class="controls"><input id="arg1" name="arg1" size="30" type="text"/><br /></div>
                <label class="control-label">   Relation </label>
                    <div class="controls"><input id="rel" name="rel" size="30" type="text" /><br /></div>
                    <label class="control-label">   Argument2 </label>
                    <div class="controls"><input id="arg2" name="arg2" size="30" type="text"/><br /></div>
                </div>
                <div class="control-group">
                    <div class="controls"><button id="exec" class="btn btn-primary"> Search </button></div>
                </div>

    </fieldset>
</form>
<div class="res"></div>
{% endblock %}

results.html:

{% if selected %}

<div class="accordion" id="accordion2">
        {% for rel in selected %}
            <div class="accordion-group">
              <div class="accordion-heading">
                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" data-target=".res#collapse{{ rel.id }}">
                  {{ rel.arg1 }} / {{ rel.rel }} / {{ rel.arg2 }}
                </a>
              </div>
              <div id="collapse{{ rel.id }}" class="accordion-body collapse">
                <div class="accordion-inner">
                  {{ rel.sentence }}
                </div>
              </div>
            </div>
        {% endfor %}
        {% else %}
                    <p>No matching relations are are available.</p>
</div>
{% endif %}

When I copy the generated html to another file collapsable works, but it doesn't work when it is used with .html(). All the necessary files are linked.

Upvotes: 2

Views: 3656

Answers (2)

Kato
Kato

Reputation: 40582

You need to enable it manually via javascript:

$(document).ready(function() {
    $("#exec").click(function() {
            var dataString = 'arg1='+ $("input#arg1").val() + '&rel=' + $("input#rel").val() + '&arg2=' + $("input#arg2").val();  
            //alert (dataString);return false;  
            $.ajax({  
                type: "GET",  
                url: "/query",  
                data: dataString,  
                success: function(data) {  
                    $(".res").html(data).collapse(); // add collapse!
                }  
            });  
            return false; 
  });
});

Upvotes: 1

lbstr
lbstr

Reputation: 2832

You will have to call the collapse function again once the new content is loaded. So...

$.ajax({  
    type: "GET",  
    url: "/query",  
    data: dataString,  
    success: function(data) {  
        $(".res").html(data).collapse();
    }  
});  

Upvotes: 2

Related Questions