0

I know some of the quirks of injecting HTML via JavaScript are subtle, but this one produces results which don't even make sense. I'm guessing some of the double quote characters are being misinterpreted, but the guides I've read don't seem to suggest doing anything different.

This is a function which injects a series of dynamically labelled buttons into a bootstrap panel and then injects that panel into a row on the page. So far I'm only producing 10 buttons, and I know I need to address the problem of having more than 12 and making a new panel. For now, the button creation is the issue.

function tableMaker(data, tableID) {
var panel = '<div class="col-md-12" style="height: 140px"> \n\t<div class="panel panel-default" style="height: 100%"> \n\t\t<div class="panel-heading">Apps Alive</div> \n\t\t <div class="panel-body">';
var mytable       = '';
var colStart      = '<div class="col-md-1"><center><b>';
var titleEnd      = '</b></center>';
var buttonStart   = '<center><button id="';
var buttonMiddle1 = '-appcheck" type="button" class="btn btn-default btn-circle btn-lg" title="';
var buttonMiddle2 = '" onclick="templateLoader("';
var buttonMiddle3 = '")"';
var buttonEnd     = '-appcheck-icon" class="fa fa-exclamation-circle"></i></button></center>';
var colEnd        = '</div>';
var panelEnd      = '\n</div>';


var i = 0, j = 0, colLimit = 12;
for(; i < data.length;){
    for (var j = 0; j < colLimit && i < data.length; ++j, ++i) {
        /*
         * Template
         * <button id="OPS-appcheck" type="button" class="btn btn-default btn-circle" title="OPS" onclick="templateLoader("OPS")"><i id="OPS-appcheck-icon" class="fa fa-exclamation-circle"></i>
         * </button>
         */
        mytable +=  colStart + data[i][2] + titleEnd + '\n' + buttonStart + data[i][2] + buttonMiddle1 +
                    data[i][2] + buttonMiddle2 + data[i][2] + buttonMiddle3 + '><i id="' + data[i][2] +
                    buttonEnd + colEnd;
    }
}

panel += mytable + panelEnd;

document.getElementById(tableID).innerHTML = panel;
}

To me, this looks right. Every quote and tag has a matching closer where it should be, but browsers turn the HTML generated into this odd thing:

enter image description here

The 'type' isn't in remotely the correct spot, and I don't know how it managed to get there based on the string format. There are also a few extra double quotes, but visual inspection doesn't tell me how they got there.

EDIT So, escaping the suggested double quotes has produced the following:

enter image description here

That seems like it should work, but the onclick isn't doing as I'd expect. It's probably a regex issue with my MVC mapping, but maybe it's still the JavaScript.

At the top of the HTML page, jquery.js and then dashboard.js are invoked. At the top of dashboard.js is the following:

function templateLoader(appTag) {
    $.get('/app/' + appTag);
}

And in the controller for the project (the only one so far), I have this:

@RequestMapping(value = "/app/*", method = RequestMethod.GET)
public ModelAndView generic()
{
    return new ModelAndView("template");
}

Yet, if I click any of the buttons, I get no reported request according to the Tomcat logs, and I'm not being redirected to my target page. Is it the mvc, or the script? There are no errors in the dev tools console either.

7
  • 1
    For now, the button creation is the issue - in what way? what do you expect, what do you see instead Commented Nov 18, 2016 at 3:07
  • Sorry, I keep hitting enter when doing the tags. It's been edited and completed. Refresh :) Commented Nov 18, 2016 at 3:09
  • 1
    the problem is with the double quotes here - templateLoader("' and here var buttonMiddle3 = '") ... change those two only to \' Commented Nov 18, 2016 at 3:17
  • Thanks for that! There's one more tiny quirk if you have the time. See the edits. Commented Nov 18, 2016 at 3:31
  • @patrickjp93 If you stick a breakpoint (using chrome debugger) in templateLoader function, you can determine if it is actually being called or not Commented Nov 18, 2016 at 3:37

1 Answer 1

1

If you console.log(panel) at the end of your function, it becomes very clear

change these two vars

var buttonMiddle2 = '" onclick="templateLoader(\'';
var buttonMiddle3 = '\')"';
Sign up to request clarification or add additional context in comments.

2 Comments

See the new edits. That has fixed the main issue, but the onclick function isn't functioning.
Nope, hence the quirkiness. ;)

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.