-1

I have a list of about 8 or so buttons that that list different categories of shops (Menswear, Food, Sports etc) and I want to display a list of shops for each category when I click on them, but using the same area of the page. So it would replace the 'sports shops' with 'Food shops' when I click on the "Food Shops" button.

How would I do this fairly simply? (I'm a noob, try to keep it simple if you can)

7
  • show your code........ Commented Apr 24, 2018 at 10:42
  • 2
    You want to use the javascript tag, not the java one. It will make your question easier to find. Commented Apr 24, 2018 at 10:43
  • Welcome to stack overflow. I am going to follow this up with a comment that contains some links and information for you to read. In short, this is a very common question and doesn't need to be asked again. You'll learn that you need to do some research before posting questions on here. Commented Apr 24, 2018 at 10:53
  • Please, do more research then post what you've tried with a clear explanation of what isn't working and provide a Minimal, Complete, and Verifiable example. Read How to Ask a good question. Be sure to take the tour and read this. Commented Apr 24, 2018 at 10:53
  • Possible duplicate of HTML/Javascript change div content Commented Apr 24, 2018 at 10:57

2 Answers 2

6

You can try this simple javascript to change the text onclick

 <div id="chgtext">This is my current text</div>
    <a href="#" onclick="document.getElementById('chgtext').innerHTML='Change the text using javascript';">Change text</a> &nbsp; &nbsp;
    <a href="#" onclick="document.getElementById('chgtext').innerHTML='Text will be changed based on the other text';">Change text2</a>&nbsp;  &nbsp;
    <a href="#" onclick="document.getElementById('chgtext').innerHTML='This is another sample changed text on click the onther text';">Change text3</a>&nbsp;  &nbsp;
    </div>

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

Comments

0

The above answer gets the job done, but here's an example of how to do this in a somewhat reusable way, so you don't have that repeated document.getElementById('chgtext').innerHTML= code.

This also shows you that the onclick attribute will allow you to run or call JavaScript code.

function changeText(text) {

  document.getElementById('chgtext').innerHTML = text;

}
<div id="chgtext">This is my current text</div>

<a href="#" onclick="changeText('Change the text using javascript')">Change text</a> &nbsp; &nbsp;
<a href="#" onclick="changeText('Text will be changed based on the other text')">Change text2</a>&nbsp;  &nbsp;
<a href="#" onclick="changeText('This is another sample changed text on click the onther text')">Change text3</a>&nbsp;  &nbsp;

Comments

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.