mhartington
mhartington

Reputation: 7025

setting variables in javascript

I need to create on snippet of javascript that, based on an A tags id, the window will navigate to the proper html file. I've got something together that when I look at it, it should work but for some reason it doesnt. Here's what I got.

<script> 
    $(document).bind('pageinit', function() { 
        $('a').each(function (index){
            var elementId = $(this).attr("id"); 
            elementId= elementId + '.html';
            $(function(){ 
                $(elementId).click(function (event) { 
                    event.preventDefault(); 
                    window.location.assign(elementId); 
                }); 
            }); 
        });
    }); 
</script>

This part is so that I can load an external html in an ios web app with out exiting the web app window

$(function(){ $(elementId).click(function (event) { 
    event.preventDefault(); 
    window.location.assign(elementId); 

Did I write the variable wrong some how? Any help would be appreciated

Upvotes: 0

Views: 61

Answers (2)

adeneo
adeneo

Reputation: 318332

I'll take a wild guess:

$(function(){ 
    $('a').on('click', function(e) {
        e.preventDefault();
        window.location.assign(this.id + '.html'); 
    });
});

Upvotes: 2

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

This is a simplified version of what you have there...

<script>
    $(function() {
        $("a").on("click", function(e) {
            e.preventDefault();
            window.location.href = this.id + ".html";
        });
    });
</script>

Upvotes: 1

Related Questions