4

I have a dropdown box name Status added in sharepoint page which have option (choose.. , In progress, Closed). Where Choose.. is bydefault value. When I open the page the value Choose.. should get change automatically to In progress.

Like if dropdown status == Choose value should get change to In progress while page load. Can someone provide me javascript to perform this action.

What I have tried:

<script src="/jquery-1.10.2.min.js"></script> 
<script type='text/javascript'> 
$(document).ready(function(){ 
    if($('#drStatus').find("option:selected").val() == "Choose..") { 
        $('#drStatus').val("In Progress"); 
    } 
}); 
</script> 
  • what is your SharePoint page? Is it a custom application page or a custom web part built in Visual Studio or else? Please add more details and tags to your question also the SharePoint version (2010 or 2013) :) – Zakir HC Apr 13 '15 at 11:34
  • 1
    This is custom webpart created in visual studio – Brishal Apr 13 '15 at 12:26
  • <script src="/jquery-1.10.2.min.js"></script> <script type='text/javascript'> $(document).ready(function(){ if($('#drStatus').find("option:selected").val() == "Choose..") { $('#drStatus').val("In Progress"); } }); </script> But this is not working now – Brishal Apr 13 '15 at 13:46
  • How are you creating the dropdown in Visual Studio? Is it an ASP DropDownList? – wjervis Apr 13 '15 at 16:37
  • dropdown is created in webpart user control and taking data from database – Brishal Apr 14 '15 at 7:14
3
$(document).ready(function(){
if($("select[title='_title of column_'] option:selected").val() == "Choose.."){
$("select[title='_title of column_']").val("In progress...");
}
});

not tested, but something like that should do it

  • Fixed your quotes – wjervis Apr 13 '15 at 11:59
  • And just a note, if this is SP2013/SPO, if the field is a required field, then the title would be _title of column_ Required Field. – wjervis Apr 13 '15 at 12:04
  • This is not working.. just one question field display name is "Status" and ID is drstatus which I have to use in Title. Thanks – Brishal Apr 13 '15 at 12:38
  • Hi user21081... look at your sourcecode with developer tools (F12)... you can see the title attribute there... – domsen123 Apr 13 '15 at 14:51
  • @user21081 What code did you use that did not work? And what version of SP are you on? – wjervis Apr 13 '15 at 16:12
1

Try this:

$(document).ready(function(){
if($("select[title='Status']").find("option:selected").val() == "Choose..")
{
    $("select[title='Status']").val("InProgress");
}
});
  • This is not working.. just one question field display name is "Status" and ID is drstatus which I have to use in Title. Thanks – Brishal Apr 13 '15 at 12:38
  • 1
    If drstatus is your dropdown ID then then You can Use $("#drstatus") as a selector instead of $("select[title='Status']") in above answer. – Rohit Waghela Apr 13 '15 at 13:15
1

If you have implemented a custom web part then why not set your drop-down value through your C#/VB.net code?

I will give you an example of a C# code on how you can change the default value in a drop-down list on page load event. In below code "status" is your drop-down list name.

protected void Page_Load(object sender, EventArgs e)
{
    string selectedValue = status.SelectedValue.ToString();
    if(selectedValue == "Choose")
    {
       //This is very simple but will result in an error if the dropdown already has a 
       status.SelectedValue == "In Progress"

       or
       //add any of below code which should not result in an error in case there already is a selected item. 

       //add error checking, just an example, FindByValue may return null
       status.Items.FindByValue("Yourvalue").Selected = true;

       or
       //add error checking, just an example, FindByText may return null
       status.Items.FindByText("In Progress").Selected = true;
    }
}
1

If an asp.net control is used inside some content area in a webpart, I guess while rendering ID will get changed. So the proposed solution can be, Give your dropdown a unique class name

Assumed mark-up is below

<asp:DropDownList ID="drStatus" runat="server" CssClass="UniqueClassName">
    <asp:ListItem Text="Choose" Value="-1"></asp:ListItem>
    <asp:ListItem Text="In Progess" Value="1"></asp:ListItem>
    <asp:ListItem Text="Closed" Value="2"></asp:ListItem>
</asp:DropDownList>

jQuery Code

//set which text you want find
var textToFind = 'In Progress';

$(".UniqueClassName > option").each(function() {
    if (this.text === textToFind) {
        $(this).prop('selected', 'selected');
    }
});
0

I was stuck on this too. You can do something like this:

$('select[title="Origin State"] option:contains("GA")').prop({ selected: true });

Be wary of the contains you may need eq depending on the content of your list. (GAP contains GA). I only posted with the contains because I have it verified in extant code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.