0

I am using jquery .load() function to load external .html files into my index.html page.

Here is my index.html:

index.html

<!doctype>
<html>
<head>

    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1">    

</head>

<body>

    <div id="nav"></div>





<script src="jquery/jquery.min.js"></script>


<script>


    $(function() {

        $("#nav").load("nav.html");

    });



    function changeLayout(layout) {

        if(layout == '4') {

    $("[class^='col-md-']").removeClass (function (index, css) {
        return (css.match (/(^|\s)col-md-\S+/g) || []).join(' ');
    }).addClass("col-md-4");



} else

        if(layout == '6') {

    $("[class^='col-md-']").removeClass (function (index, css) {
        return (css.match (/(^|\s)col-md-\S+/g) || []).join(' ');
    }).addClass("col-md-6");



}

else

        if(layout == '12') {

    $("[class^='col-md-']").removeClass (function (index, css) {
        return (css.match (/(^|\s)col-md-\S+/g) || []).join(' ');
    }).addClass("col-md-12");



}

}


</script>




</div>

</body>
</html>

This is the nav.html file that I'm loading:

nav.html

<div id="subnav" class="container">
    <div class="row">
        <div class="btn-wrapper">
            <div class="layout">
                <button onclick="changeLayout('4');">Layout1</button>
                <button onclick="changeLayout('6');">Layout1</button>
                <button onclick="changeLayout('12');">Layout1</button>
            </div>
        </div>
    </div>
</div>

My problem is that the changeLayout function above will only work 'Once' and then I would need to reload the whole page to get it to work again.

Why is this happening and how can I fix this problem?

2 Answers 2

1

You need to add type="button" to your buttons currently your buttons seem to be submitting the page.

Also no need for href attribute in buttons.

<button type="button" onclick="changeLayout('4');">Layout1</button>
<button type="button" onclick="changeLayout('6');">Layout1</button>
<button type="button" onclick="changeLayout('12');">Layout1</button>

Move you changelayout function to nav.html

Sign up to request clarification or add additional context in comments.

6 Comments

@estevangomez can u check console if there is any error ?
@estevangomez can you show html of you nav page ?
The source code is the same as the one I've posted above in the question. .load() include the html into the DOM so source code will not show the included html
But you will have the html of nav.html file can u share that ?
OK got it move your function changelayout into nav.html.
|
0

You should place your scripts in the HTML head section. Just move both of your script tags in the head section (below title) and you should be fine.

I guess you replaced the content of your body with the loaded html file - so all your JavaScript was gone.

1 Comment

Tried it and I still have the same issue...Didn't sort the problem

Your Answer

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