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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:36:02+00:00 2026-06-01T22:36:02+00:00

I have the following: <div class=btn-group> <input disabled=disabled id=dialogType> <button data-toggle=dropdown class=dropdown-toggle> <span class=caret></span>

  • 0

I have the following:

<div class="btn-group">
    <input disabled="disabled" id="dialogType">
    <button data-toggle="dropdown" class="dropdown-toggle">
    <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" id="towns">
        <li><a data-value="0Z" href="#">A</a></li>
        <li><a data-value="10" href="#">B</a></li>
    </ul>
</div>

<div class="btn-group">
    <input disabled="disabled" id="dialogStatus">
    <button data-toggle="dropdown" class="dropdown-toggle">
    <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" id="places">
        <li><a data-value="22" href="#">C</a></li>
        <li><a data-value="33" href="#">D</a></li>
    </ul>
</div>

In the past I used the following javascript to populate the #town label field when one of the links is clicked:

$('#towns > li > a').click(function (e) {
    e.preventDefault();
    $('#town').text($(this).html())
 });

Now I would like to make this javascript work for any similar ul that’s inside of a DIV with the class dropdown-menu.

So what I would like is:

  • When the user clicks on a link such as A then the first input element inside the btn-group is given the value of A and also a data-value of “0Z”
  • When the user clicks on a link such as D then the first input element inside that btn-group is given the value of D and also a data-value of “33”

Can someone tell me how I could do this. I just need a more generic version of the function above.
.

  • 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-01T22:36:04+00:00Added an answer on June 1, 2026 at 10:36 pm

    I’d suggest, though currently untested:

    $('.dropdown-menu a').click(
        function(e){
            e.preventDefault();
            var that = $(this),
                dValue = that.attr('data-value');
            that.closest('.btn-group').find('input:first').attr('data-value',dValue).val(dValue);
        });
    

    Essentially the way this works is:

    1. clicking the link prevents the default action,
    2. finds the closest .btn-group (ancestor) element,
    3. finds the first descendant input element within,
    4. assigns the data-value (assigned to the dValue variable) to the data-value attribute of that input, and finally
    5. assigns the value of dValue to be the value of that input.

    Edited in response to comment left by OP:

    …I will have more HTML with button groups loaded with Ajax. Sorry I should have said that. Because of that should I also use “on”…

    Because the btn-group elements are added dynamically, I’d suggest the following change:

    $('#idOfBtnGroupParentElement').on('click','.dropdown-menu a',
        function(e){
            e.preventDefault();
            var that = $(this),
                dValue = that.attr('data-value');
            that.closest('.btn-group').find('input:first').attr('data-value',dValue).val(dValue);
        });
    

    References:

    • attr().
    • closest().
    • find().
    • on()
    • val().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following HTML: <div class=container> <span class=remove>remove</span> </div> and jquery: $(.container).delegate(.remove,
I have the following HTML markup: <div id=step1 class=step> <h1>Enter code</h1> <p><input type=text value=<%=
I have the following code: <div class=modal-footer> <a class=btn btn-primary onclick=saveChanges ();>Save changes</a> </div>
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
So I have some html that looks like the following: <div class='something unknown' id='something_unknown_1'>
I have the following div and paragraphs: <div id=container> <p class=jsavoutput jsavscroll readonly=readonly style=width:225px;
I have following PHP code $val=<div id=user.$row['cid']. userid=.$row['cid']. class=innertxt><img src=images/images.jpg width=50 height=50><strong>.$uname.</strong><ul> <li>Email: .$row['cemail'].</li>
onI have the following html structure: <div> <ul> <li class=gravity-form></li> <li class=gravity-form-divider></li> </ul> </div>
I have following div: <div id = zone-2fPromotion-2f class = promotion> How can I
I have the following HTML: <div class=wrapper> <div class=label> foor bar baz </div> <img

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.