Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9227165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:00:10+00:00 2026-06-18T05:00:10+00:00

The following code triggers just before a page is loaded. Now I’ve managed to

  • 0

The following code triggers just before a page is loaded. Now I’ve managed to fill a select with values. But I’m not sure on how to make the first value to be the deafault selected value.

$(document).on('pagebeforeshow', '#searchpage',
//This function is the whole function that runs when the pagebefore event occurs
function () {
    //This reads the universities from the api we created
    $.getJSON(AddressAccess + "Home/university/format/json",

    function (data) {
        //The data is sent back in a collection of objects. We need to extract each object and do relative operations
        for (var i = 0; i < data.length; i++) {
            var university = data[i];
            var SelectDropDown = document.getElementById("searchuniversity");
            var NewOption = new Option(university.Name, university.UniverstiyID);

            if (i == 1) {
                SelectDropDown.add(NewOption, Selected);
            } else {
                SelectDropDown.add(NewOption);
            }

        };
    });
});

Now if i use SelectDropDown.add(NewOption,Selected); Only one option is made as an option in the select and what I want is to just make the first option being read from my json data to be the default option appearing in the select.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:00:12+00:00Added an answer on June 18, 2026 at 5:00 am

    Set selectedIndex of SelectDropDown to 1:

    if (i == 1) {
      SelectDropDown.add(NewOption);
      SelectDropDown.selectedIndex = 1;
    }
    

    If you’re really talking about the very first option from the options list, selectedIndex should be 0. The options array of a select element is zero based.

    [edit] based on your comment:
    maybe the select already contains a blank option. Try:

    if (i < 1) {
      SelectDropDown.options.length = 0;
      SelectDropDown.add(NewOption);
      SelectDropDown.selectedIndex = 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does the following code get the runtime error: Members of the Triggers collection
I am using following PHP code for trigger creation but always get error, please
I have the following XAML-Code (for testing, so it is not a nice-Code): <Style
I have the following code being run if an iphone/ipad/ipod is detected: $('.tooltip-trigger').tooltip({offset: [20,
In the following code, if Control (the element that trigers Toggle 's first OL
Consider the following code: $(window).unload(function () { console.log('foo'); }); If I trigger the unload
Following code produces a nested array as a result for keys containing three items:
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Following code: <%= render 'shared/error_messages', f.object %> where f.object is instance of a class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.