Haminteu
Haminteu

Reputation: 1334

Trigger Input click event when Label Text Change

  1. I have a label with id lblResult

  2. I have the following code:

    @(Html.TreeView(Model) .EmptyContent("root") .Children(m => m.Childs) .HtmlAttributes(new { id = "tree" }) .ChildrenHtmlAttributes(new { @class = "subItem" }) .ItemText(m => m.AssetNumber) .ItemTemplate( @ @*@item.AssetNumber*@ ) )
  3. Then, I have the following code on the view:

@using (Html.BeginForm("AssetTypeIndex", "ControlFiles", FormMethod.Get))
{
<div class="row">
<div class="col-lg-offset-2 col-lg-8 col-lg-offset-2">
<div class="form-inline">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">Search</span>
@Html.TextBox("SearchString", ViewBag.CurrentFilter as string, new { @class = "form-control" })
<span class="input-group-btn">
<button class="btn btn-default" type="submit" value="Search">View</button>
</span>
</div>
<div class="pull-right">
<a [email protected]("AssetTypeCreate", "ControlFiles") class="btn btn-default">Create New</a>
</div>
</div>
</div>
</div>
}

  1. On script section:

    $(function () { var selectedData; $('#jstree').jstree({ "core": { "multiple": true, "check_callback": true, 'themes': { "responsive": true, 'variant': 'small', 'stripes': false, 'dots': true } }, "types": { "default": { "icon": "glyphicon glyphicon-record" }, "root": { "icon": "glyphicon glyphicon-ok" } }, "plugins": ["dnd", "state", "types", "sort"] }).on('changed.jstree', function (e, data) { var i, j, r = []; for (i = 0, j = data.selected.length; i

If you see on the code above. There's a submit button which is trigger the search function on the controller.
My question is, Is it possible to run the 'Search Function' without clicking the button? I want to run the 'Search Function' when lblResult received the text from jsTree, it means when lblResult text is change.

Please advice.
Thank you.

Upvotes: 0

Views: 351

Answers (2)

Bai Nguyen
Bai Nguyen

Reputation: 834

Sorry for my late reply, when you click on a node in your jsTree ex: a tag have id is node1, you can remove this code

<span class="input-group-btn">
     <button class="btn btn-default" id="submitSearch" type="submit" value="Search">View</button>
 </span>

or add this css code

#submitSearch{display:hidden}

I edited a bit in your code

$('#jstree').on('changed', function (e, data) { 
    var i, j = data.selected.length, r = []; 
    for(i = 0,  i < j; i++) { 
        r.push(data.instance.get_node(data.selected[i]).text); 
    } 
    $('#event_result').html('Selected: ' + r.join(', ')); 
    $('#lblResult').text('your text here');//
    $('form').submit();
});

Upvotes: 0

Prabhu
Prabhu

Reputation: 1057

Option 1: When clicking a node on your jsTree trigger that submit function by simulating a mouse-click using click method. As you know for sure the label will be changing.

@using (Html.BeginForm("AssetTypeIndex", "ControlFiles", FormMethod.Get)){
    <div class="row">
        <div class="col-lg-offset-2 col-lg-8 col-lg-offset-2" style="margin-top:-5px;">
            <div class="form-inline">
                <div class="input-group">
                    <span class="input-group-addon" id="basic-addon1">Search</span>
                    @Html.TextBox("SearchString", ViewBag.CurrentFilter as string, new { @class = "form-control" })
                    <span class="input-group-btn">
                        <button class="btn btn-default" id="submitSearch" type="submit" value="Search">View</button>
                    </span>
                </div>

                <div class="pull-right">
                    <a [email protected]("AssetTypeCreate", "ControlFiles") class="btn btn-default">Create New</a>
                </div>
            </div>
        </div>
    </div>
}
$("#submitSearch").click(); // use the code when clicking a node on jsTree

Option 2: If you are manually changing the label by entering in a textbox then use the keyup event to trigger submit function.

$(function(){
  $("#lblreasult").on('keyup', function(){
    $("#submitSearch").click();
  });
});

Upvotes: 0

Related Questions