I'm using a bootstrap dropdown menu, and I need to set the default option (Highest positive votes) when the user reload the page.
I tried it $('#dropdownMenuButton span.option').text("Highest positive votes");, but it didn't solve my problem because I can't keep the text on the js file, I need to keep the text on the html file due the translation of the django framework {% trans 'Highest positive votes' %}
HTML Code
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown">
<span>{% trans 'Sorted by' %}: </span><span class="option">{% trans 'Highest positive votes' %}</span>
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item itemSort" href="#" name='thumbsUp'>{% trans 'Highest positive votes' %}</a>
<a class="dropdown-item itemSort" href="#" name='thumbsDown'>{% trans 'Highest negative votes' %}</a>
<a class="dropdown-item itemSort" href="#" name='creationOrder'>{% trans 'Latest' %}</a>
</div>
Jquery
$('a.itemSort').click(function() {
if (this.name == 'thumbsUp') {
...
} else if (this.name == 'thumbsDown') {
...
} else if (this.name == 'creationOrder') {
...
}
$('#dropdownMenuButton span.option').text($(this).text());
});
I expect that when I relaod the page, the dropdown-menu show the text Highest positive votes.
I'm trying to simulate the click on the thumbsUp option on the dropdownMenuButton.
How can I do that? There's a better way to solve it?
ps.: I tried implement the solutions that I read in another questions similar to that. But it's not the same problem.
$("#dropdownMenuButton").on('click', parameter). But it didn't work. You're right, the same id is not correct, I'm gonna fix it.