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 7725161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:52:13+00:00 2026-06-01T04:52:13+00:00

I’m trying to select the option based on the php variable value. So I

  • 0

I’m trying to select the option based on the php variable value. So I need option value=1 is selected in the first <select> and option value=6 selected in second . Since I have a bunch of other <select> in the form, eventually, I will have an php array of some values, and I need each of the value match up with the option value under corresponding index in the a group of <select> that sharing the same class='si_1', and assign selected to that option.

Thanks

php part

<?php 
$select_1 = "1";
$select_2 = "6";
?>

JQuery part

$(document).ready(function(){
    var sel_class = $(".si_1");
    var array = {"<?php echo $select_1?>","<?php echo $select_2?>"};
    JQuery.each(array, function(index, value) {
        var current = sel_class.get(index);
        $(current + " option[value=" + value + "]").attr('selected', 'selected');
    });
});

I don’t know if $(current + " option[value=" + value + "]") is the correct syntax to target the <select> at index in $(".si_1") that has option where the option attribute value has value that’s at index in array

HTML part

<select class="si_1">
    <option value="1">Test 1</option>
    <option value="3">Test 3</option>
    <option value="5">Test 5</option>
    <option value="7">Test 7</option>
</select>
<select class="si_1">
    <option value="2">Test 2</option>
    <option value="4">Test 4</option>
    <option value="6">Test 6</option>
    <option value="8">Test 8</option>
</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-01T04:52:15+00:00Added an answer on June 1, 2026 at 4:52 am

    You can do this in the JavaScript:

    selectedVal1 = "<?= $select_1; ?>";
    selectedVal2 = "<?= $select_2; ?>";
    

    When the client looks at it, it will look like this:

    selectedVal1 = "1";
    selectedVal2 = "6";
    

    Then it’s just using jQuery to set the selected attribute to selected (Just do .attr(‘selected’, ‘selected’))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
this is what i have right now Drawing an RSS feed into the php,
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.