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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:14:37+00:00 2026-05-26T18:14:37+00:00

On a click event, I want to add a fragment of html code which

  • 0

On a click event, I want to add a fragment of html code which includes a select/option box, to a page. First I get the appropriate location to put it, then take fragment of code in a javascript string, turn it into a jquery object. Next I want to select the appropriate value in the select/option box. In this example, I am having trouble targeting the correct select/option box.

tplReasons = "<td>Reason</td>\n\
    <td class='none'>\n\
    <div data-role='fieldcontain' class='select'>\n\
        <select class='myClass' name='select-choice-1' id='select-choice-1'>\n\
            <option value='01'>option 1</option>\n\
            <option value='02'>option 2</option>\n\
            <option value='03'>option 3</option>\n\
        </select>\n\
    </div>\n\
    </td>\n";



$('.my_class').live('change', function (){  
   var MyLocation = $('some_div');
   var MytplReasons = $(tplReasons);                        
   if(someCondition == 2){
     $('MytplReasons .myClass option').get(2).attr('selected', 'selected');
   }
   // (add jquery mobile formatting)
   MytplReasons.appendTo(MyLocation).trigger('create');                 
});     

</script>
  • 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-05-26T18:14:37+00:00Added an answer on May 26, 2026 at 6:14 pm

    Assuming I’ve understood what you’re trying to do, there are several issues with your code. What I think you’re trying to do is set the selected option in the HTML fragment (presumably you’re then going to append that HTML fragment to the DOM).

    This is the line that causes the problems:

    $('MytplReasons .myClass option').get(6).attr('selected', 'selected');
    

    Firstly, the selector starts with MytplReasons, so jQuery is going to look for an element of type MytplReasons, and that doesn’t exist. What you wanted to do was to find elements matching .myClass option, in the HTML fragment contained in MytplReasons. To do that you can use MytplReasons as the context:

    $('.myClass option', MytplReasons)
    

    Secondly, get returns the underlying DOM element, not a jQuery object. DOM elements don’t have an attr method. You need to use eq instead:

    $('.myClass option', MytplReasons).eq(6).attr('selected', 'selected');
    

    A further problem (perhaps just because you have shortened the code in the question) is that there isn’t a matching element at index 6 (there are only 3 option elements in your fragment).

    A couple of other issues:

    1. You are missing a semi-colon after the declaration of MyLocation
    2. MyLocation is already a jQuery object. You don’t need to pass it into jQuery again to use the html method

    Here’s a working example of your code (modified slightly, so the HTML fragment is appended to a table, so you can actually see the result).

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

Sidebar

Related Questions

I want to attach a 'click' event handler to the first child of an
I have a linkbutton in gridview and i want to click event on code
I have a table that I want to add a click event to the
hi i have a image and i want to add a click event to
I have the following ajax.actionlink. i want to add click event to this actionlink.
Please help. I want to add a click event on checkbox that i created
i want to add a row in a table on the click event of
I want to add the click event to all elements where the `id=violacao: $(document).ready(function
I subclassed TTTableMessageItem to show user response, and I want to add click event
So basically I want to add a javascript function to a button's Click event

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.