2

I have a bootstrap select dropdown.

I want to change the selection when I load the page. How can I write jquery for the same?

I've tried using $('.dnsType').toggle('Hostname') but that didnt work :(

This is my code:

<div class="row form-group">
    <div class="btn-group dnsType <%= getColumnSize(editMode) %>">
        <button class="btn btn-xs white dropdown-toggle" id="dnsType" type="button" data-toggle="dropdown" aria-expanded="true">
            <span data-value="IP Address">IP Address</span><b class="caret"></b>
        </button>
        <ul class="dropdown-menu" role="menu" aria-labelledby="dnsType">
            <li><a value="IP Address">IP Address</a></li>
            <li><a value="Hostname">Hostname</a></li>
        </ul>
    </div>
    <div class="<%= getColumnSize(editMode) %>">
        <div class="form-horizontal">
            <input type="text" name="ipAddress" class="ipAddress" data-name="IP Address" class="form-control" value="<%= controller.ipAddress %>">
            <span class="validationErrorMessage"></span>
        </div>
    </div>
</div>
2
  • 1
    'dnsType' is the value for attribute aria-labelledby, not a classname Commented Nov 22, 2016 at 23:52
  • Bootstrap doesn't have a toggle function. Jquery has a toggle function but it hides and shows an element. So I don't think it does what you want it to do. Commented Nov 23, 2016 at 0:02

1 Answer 1

2

Bootstrap dropdowns are not like <select> elements. They don't have "selections". It is just a dropdown that shows a list of links when you click the button. I'm guessing that you just want to change the text for that button when the page loads. Here is how you can do that:

$("#dnsType span").text("Hostname").attr('data-value','Hostname');

This sets the text to "Hostname" and changes the data-value attribute to "Hostname"

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.