This is a migrated thread and some comments may be shown as answers.

[Solved] Set focus of html element

4 Answers 99 Views
TabStrip
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Stéphan Parrot
Top achievements
Rank 1
Stéphan Parrot asked on 20 Apr 2011, 10:02 PM
Hello folks,
   We're using the TabStrip control on a page that are loading their content as follow:
Html.Telerik().TabStrip()
   .Name("tabs")
    .Items(tabstrip =>
    {
      tabstrip.Add()
            .Text(ViewRes.ProductStrings.InventoryCatalog)
            .LoadContentFrom("AdvancedSearchInventory", "Product")
            .HtmlAttributes(new { id = "tabInventory" });
 
      tabstrip.Add()
            .Text(ViewRes.ProductStrings.ChartSearch)
            .LoadContentFrom("AdvancedSearchChart", "Product")
            .HtmlAttributes(new { id = "tabChartSearch" });
 
      tabstrip.Add()
            .Text(ViewRes.ProductStrings.MarineXRef)
            .LoadContentFrom("AdvancedSearchMarineXRef", "Product")
            .HtmlAttributes(new { id = "tabMarineXRef" });
 
      tabstrip.Add()
            .Text(ViewRes.ProductStrings.PartsXRef)
            .LoadContentFrom("AdvancedSearchPartsXRef", "Product")
            .HtmlAttributes(new { id = "tabPartsXRef" });
      })
      .ClientEvents(events => events
      .OnContentLoad("onTabContentLoad")
      .OnSelect("onTabContentLoad"))
      .SelectedIndex(ViewData["SelectedTab"] == null ?
            0 :
            Convert.ToInt32(ViewData["SelectedTab"]))
      .Render();
The problem we experience is thhe following:
   When we first arrive on the page that displays the tabstrip, the first tab loads it's content an set the focus on a keyword textbox.
If we click on another tab, it loads the content of it as described above. Now, we click back on the first tab that was previously selected and which the keyword textbox had the focus but, this time, no focus was set.
We tried alot of things to make it work but the ONLY way I've came up with is to use HTML5's new autofocus property but it only works in new gen browsers that supports html5... Since our client base is mainly still using IE6, 7 and 8 and are very relunctant on using Chrome, Firefox, Opera or Safari, we're a bit stumped on this one.
Anyone has a WORKING suggestion for us?
Thanks a lot!

Stéphan

4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 21 Apr 2011, 07:13 AM
Hello Stéphan Parrot,

 How is the textbox focuesed initially? The same code should be used when a tab is selected in order to bring the focus back.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Stéphan Parrot
Top achievements
Rank 1
answered on 22 Apr 2011, 01:17 PM
Hi Atanas,
Here's the javascript code:
function onTabContentLoad(e) {
    var item = $(e.item);
    $('span#error').html('');
 
    switch (item.attr('id')) {
        case "tabInventory": $('#Keyword').focus();
            break;
        case "tabChartSearch": $('#SelectedMajorGroup').focus();
            disableCbo();
            break;
        case "tabMarineXRef": $('#KeywordMarine').focus();
            break;
        case "tabPartsXRef": $('#KeywordXref').focus();
            break;
    }
 
}

And here's the HTML of the first tabstrip part named
tabInventory (see the tabstrip declaration in my first post):
<div class="divDetailContainerLeft">
    <div>
        <%= Html.Label(ViewRes.ProductStrings.KeywordPage)%>
    </div>
    <div>
        <%= Html.Label(ViewRes.ProductStrings.Target)%>
    </div>
</div>
 
<div class="divDetailContainerRight">
    <div>
        <%= Html.TextBoxFor(t => t.Keyword,new { autofocus="autofocus"})%>
        <%= Html.ValidationMessage("Keyword")%>
    </div>
    <div>
        <%= Html.DropDownListFor(l =>
                l.SearchValueInventory, Model.comboboxInventory)
            %>
    </div>
    <div>
        <%= Html.Button(ViewRes.Shared.Submit,
                    ViewRes.Shared.Submit,
                    HtmlButtonType.Submit,
                    "",
                    new Dictionary<string, object>
                    {
                        { "class", "ui-button ui-state-default" },
                        { "id", "cmdSubmit" }
                    })%>
    </div>
</div>

As you can see, I've added autofocus to the Keyword textbox and it doe the
trick for new-gen browsers but, for old ones, it just doesn't work...

Thanks!
0
Dimo
Telerik team
answered on 22 Apr 2011, 03:11 PM
Hello Stéphan,

The OnSelect TabStrip event is fired before the corresponding tab content has been made visible. As a result, the textbox cannot be focused at that early stage. You can use setTimeout to resolve this.

Regards,
Dimo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Stéphan Parrot
Top achievements
Rank 1
answered on 22 Apr 2011, 03:45 PM
Dimo,
Thanks a lot, this solved my issue!
Best regards,

Stéphan
Tags
TabStrip
Asked by
Stéphan Parrot
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Stéphan Parrot
Top achievements
Rank 1
Dimo
Telerik team
Share this question
or