1

I am setting up some basic pagination of a table, and I have the following JS function:

function AddPagination() {
    var paginationDiv = document.getElementById("pagination");
    for (i = 0; i < 3; ++i) {
        var page = document.createElement("a");
        page.innerHTML = i + 1;
        page.setAttribute("title", i + 1);
        page.setAttribute("href", "javascript:RenderResultTable(this.innerHTML)");
        paginationDiv.appendChild(page);
    }
}

What I want to do is pass page number clicked on to the RenderResultTable method. I have this number stored as the innerHTML and title for the link element, how can I get this passed through using the above code?

Thanks.

1 Answer 1

1

Personally, I wouldn't use JavaScript for pagination but if that's the way you want to go, you need to use some string concatenation. I'm not sure what RenderResultTable() does but you can set that line up like this:

page.setAttribute("href", "javascript:RenderResultTable('" + page.innerHTML + "')");

I believe that should do the trick.

EDIT: Shouldn't you be using i++ in your loop instead of ++i? I think what you have right now will give 2 as the first page number. Please correct me if I am wrong.

EDIT: page.innerHTML will need to be escaped by this functions and then unescaped the in the RenderResultTable() function. escape() and unescape(). This is to prevent JavaScript injections and/or accidental bugs.

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

3 Comments

++i should be fine -- there's lots of discussion elsewhere as to why pre or post incrementation is more efficient (or not) in for-loops, depending on the language. Try it out: jsfiddle.net/samdutton/DpmhP
You might want to add quotes for the arg to RenderResultTable, ie "javascript:RenderResultTable('"+page.innerHTML+"')"); because you may not always have a number as the innerHTML
Perfect thanks. The reason i'm using pagination in Javascript, is that the way the code is, the table is also created in javascript. So I have to write a pagination method also. Thanks again.

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.