0

i want to change the content of my Searchbox when the checkBox is checked.

So i got following checkboxes:

<li><label style="margin-left:20px">Bars:  <input type="checkbox"></label><li>
<li><label style="margin-left:20px">Clubs:  <input type="checkbox"></label><li>

And this is my input field "Searchbox"

 <input id="pac-input" class="controls form-control" type="text" placeholder="Search Box">

When no checkbox is activated the SearchBox is clean.

Iam searching for a possibility to change the content of my checkbox to Bars when the Bars checkBox is activated and i want to change it to Clubs when Clubs checkBox is activated.

1 Answer 1

2

You can use this code if you want to do this in pure JavaScript way:
HTML:

<li><label style="margin-left:20px">Bars:  <input id="bar" type="checkbox" onclick="updateSearchBox();"></label><li>
<li><label style="margin-left:20px">Clubs:  <input id="club" type="checkbox" onclick="updateSearchBox();"></label><li>
<input id="searchBox" class="controls form-control" type="text" placeholder="Search Box">

JavaScript:

var barCheckBox = document.getElementById('bar');
var clubCheckBox = document.getElementById('club');
var searchBox = document.getElementById('searchBox');
function updateSearchBox() {
    if (barCheckBox.checked) {
        searchBox.value += ' Bars';
    } else if (clubCheckBox.checked) {
        searchBox.value += ' Clubs';
    } else {
        searchBox.value = '';
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried your way and it works, Thank you so much. But if there already text in the searchbox and i check the bars or clubs checkbox it delete the text and update it to bars and clubs .. but what i want is to add bars or clubs to the existing text. Something linke searchBox.value= searchbox text + 'Clubs';

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.