Output var value inside html string

I have this code below and a div asigned to var selectedRowContent, but I dont know how to get that var to appear inside the after brackets inside the td element

$('#js_btnExpandCollapseCategory@(Model.ID)')
.button()
.click(function () {

var selectedRowContent = $('#js_TableRowCategory@(Model.ID)');

$('#tableRowCategory2').after('<tr id="2" style="height:40px"><td colspan="3">selectedRowContent</td ></tr > ');
});

concatenate the variable with the string.

'<tr id="2" style="height:40px"><td colspan="3">selectedRowContent</td ></tr >' 

=>

'<tr id="2" style="height:40px"><td colspan="3">'+selectedRowContent+'</td ></tr >' 

Though you probably want some aspect of said variable… .html() or .value() or… whatever applicable pieceof the variable you’re trying to put in there.

1 Like

Ah yes, I went code blind then.

Shame its out putting as [object Object]

This is that var value

<div id="js_TableRowCategory@(Model.ID)" style="display:none">

    <!-- #region tblSection -->

    <table id="tblSection" class="tablesorter-bootstrap" style="table-layout: fixed;">
        <thead>
            <tr>
                <th>
                    <p class="text title textSizeStandard alignCenter">Number</p>
                </th>

                <th>
                    <p class="text title textSizeStandard alignCenter">Section</p>
                </th>

                <th>
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var section in Model.Sections)
            {
                <tr id="tableRowSection@(section.ID)">
                    @Html.Partial("~/Views/Questionnaire/_QuestionnaireSection.cshtml", section, new ViewDataDictionary { { "recordID", ViewData["recordID"] } })
                </tr>
            }
        </tbody>
    </table>

    <!-- #endregion -->

</div>

and you want to output… what, exactly?

That whole div, I posted it after I originally replied sorry

selectedRowContent.html() will give you the contents of the div.

1 Like

That’s perfect, thank you very much…

1 Like

me again, its off topic but its the same code, but wondered if you could help me with the button click function.

I want to use that button to open that div, but then the same button to delete the tr row I created, basically to close it up.

Whats the best way to do this.

This below.

<td><button id="js_btnExpandCollapseCategory@(Model.ID)" class="text textSizeStandard">+</button>


$('#js_btnExpandCollapseCategory@(Model.ID)')
.button()
.click(function () {
var selectedRowContent = $('#js_TableRowCategory@(Model.ID)');

$('#tableRowCategory@(Model.ID)').after('<tr style="height:auto"><td colspan="3">'+selectedRowContent.html()+'</td></tr> ');
});

I don’t understand what you mean by ā€˜open that div’, or ā€˜tr row i created’. You’re trying to… delete the thing you just added? Why not just…not add it in the first place? Give me an example of what you start with, and what you want to end with.

Ok this is my thinking.

what happens when you first click the button, is that I create that new tr row and populate the td row inside it with the div.

But what I need to then happen, is if they click the same button (+), it then hides that row that I created so in effect like an accordion.

Not right, but something like this

    $(document).ready(function () {

        // #region btnExpandCollapse

        var button@(Model.ID) = false;

        if (button@(Model.ID) == false) {
            $('#js_btnExpandCollapseCategory@(Model.ID)')
                .button()
                .click(function () {
                    var selectedRowContent = $('#js_TableRowCategory@(Model.ID)');

                    $('#tableRowCategory@(Model.ID)').after('<tr style="height:auto"><td colspan="3">' + selectedRowContent.html() + '</td></tr> ');
                    var button@(Model.ID) = true;
                });
        } else {
            $('#js_btnExpandCollapseCategory@(Model.ID)')
                .button()
                .click(function () {
                    var selectedRowContent = $('#js_TableRowCategory@(Model.ID)');

                    "In here i delete that tr row I created."

                    var button@(Model.ID) = false;
                });
        }

instead of creating and deleting it constantly, why not just toggle it.

Ye that would do it, think my way is huge overkill.

Could you help me get that working.

Construct the row(s) and button in-situ, and show me the resulting HTML, and we can target properly.

OK I’ve just looked at the code and its huge, so I’m wondering if my attempt below will allow us to get there, if not then just say.

<td><button id="js_btnExpandCollapseCategory@(Model.ID)" class="text textSizeStandard">+</button>

$('#js_btnExpandCollapseCategory@(Model.ID)')
.button()
.click(function (toggleFunction) {
});


function toggleFunction() {
var selectedRowContent = $('#js_TableRowCategory@(Model.ID)');
var x = $('#tableRowCategory@(Model.ID)').after('<tr style="height:auto"><td colspan="3">' + selectedRowContent.html() + '</td></tr>');
if (x.style.display === "none") {
x.style.display = "block";
    } else {
        x.style.display = "none";
    }
}

And this is the div that gets displayed

<div id="js_TableRowCategory@(Model.ID)" style="display:none">

    <!-- #region tblSection -->

    <table id="tblSection" class="tablesorter-bootstrap" style="table-layout: fixed;">
        <thead>
            <tr>
                <th>
                    <p class="text title textSizeStandard alignCenter">Number</p>
                </th>

                <th>
                    <p class="text title textSizeStandard alignCenter">Section</p>
                </th>

                <th>
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var section in Model.Sections)
            {
                <tr id="tableRowSection@(section.ID)">
                    @Html.Partial("~/Views/Questionnaire/_QuestionnaireSection.cshtml", section, new ViewDataDictionary { { "recordID", ViewData["recordID"] } })
                </tr>
            }
        </tbody>
    </table>

    <!-- #endregion -->

</div>

So i’m going to try and extrapolate a bit from what you’ve shown me.

When ā€˜expanded’, the code looks something like…

<tr id="js_TableRowCategory1"><td>SomeTDHere</td><td>SomeOtherTDHere</td><td><button id="js_btnExpandCollapseCategory1" class="text textSizeStandard">+</button></td></tr>
<tr style="height:auto"><td colspan="3">
    <table id="tblSection" class="tablesorter-bootstrap" style="table-layout: fixed;">
        <thead>
            <tr>
                <th>
                    <p class="text title textSizeStandard alignCenter">Number</p>
                </th>

                <th>
                    <p class="text title textSizeStandard alignCenter">Section</p>
                </th>

                <th>
                </th>
            </tr>
        </thead>
        <tbody>
            //This bit gets replaced/repeated a bunch of times.
            @foreach (var section in Model.Sections)
            {
                <tr id="tableRowSection@(section.ID)">
                    @Html.Partial("~/Views/Questionnaire/_QuestionnaireSection.cshtml", section, new ViewDataDictionary { { "recordID", ViewData["recordID"] } })
                </tr>
            }
        </tbody>
    </table>
</td>
</tr>
</table>

Yes?
If so, then the toggle code is as follows:

$('.js_btnExpandCollapseCategory')
.button()
.click(function () {
  $(this).parent().parent().next().toggle();
  $(this).html(($(this).html() == "+") ? "-" : "+");
});

So what does it do?
$(this) refers to the button.
.parent() refers to the TD the button is in.
.parent() (again) refers to the TR the TD is in.
.next() moves to the next sibling (the TR below the button)
.toggle() hides or shows said row, depending on its current state.

The second line toggles the button’s content from + to - and back again.

Additional steps:

  • Make the buttons classed ā€œjs_btnExpandCollapseCategoryā€, with no ID (unless otherwise required).
  • Unless required by other code, Remove the ID from TableRowCategory entirely, and instead insert the rows naturally. We don’t need it anymore, because we just tell the buttons ā€œwhatever your next row is, hide/show that.ā€ rather than doing it by ID and adding/deleting things.
  • Default the data rows to hidden.

I got you, and I don’t think it will work out in this case, as the tables aren’t structured like you have it.

This is my bit of an huge project so Im working with what I have and below is probably closer what Im going to have to work with.

var click = false;
$('#js_btnExpandCollapseCategory@(Model.ID)')
.button()
.click(function () {
if (click == false) {
    document.getElementById("js_btnExpandCollapseCategory@(Model.ID)").innerHTML = "-";
    var selectedRowContent = $('#js_TableRowCategory@(Model.ID)');
    $('#tableRowCategory@(Model.ID)').after('<tr style="height:auto"><td colspan="3">' + selectedRowContent.html() + '</td></tr>');
   click = true;
} else {
    document.getElementById("js_btnExpandCollapseCategory@(Model.ID)").innerHTML = "+";
   click = false;
}
});

Thanks for coming back to me though, I really am appreciating it.

I think I’ll have to do the create the ā€˜tr’ and delete ā€˜tr’ where I was at the start, cant see it working otherwise, nightmare.

Not even sure how to delete the ā€˜tr’ I created now to even try that

Something like this, but its then deleting itself rather than the child ā€˜tr’ I created

.button()
.click(function () {
if (click == false) {
document.getElementById("js_btnExpandCollapseCategory@(Model.ID)").innerHTML = "-";
var selectedRowContent = $('#js_TableRowCategory@(Model.ID)');
$('#tableRowCategory@(Model.ID)').after('<tr style="height:auto"><td colspan="3">' + selectedRowContent.html() + '</td></tr>');
click = true;
} else {
document.getElementById("js_btnExpandCollapseCategory@(Model.ID)").innerHTML = "+";
$('#tableRowCategory@(Model.ID):first-child').hide();
click = false;
}
});

Used this too, but it just ends up removing the #tableRowCategory@(Model.ID) rather than the child ā€˜tr’ I created

$('#tableRowCategory@(Model.ID)').closest('tr').remove();

Then tried and still it doesnt delete the next tr lol, I give up, off to bed!

$('#tableRowCategory@(Model.ID)').parent('tr').next('tr').hide();

I was sort of on the right track, and found that this worked.

$('#tableRowCategory@(Model.ID)').next('tr').hide();

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.