最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Creating a simple menu w reusable onClick function for different variables - Stack Overflow

programmeradmin0浏览0评论

So I have a simple menu for a blog that's working but I'm trying to figure out if there's a way to reuse onClick function. So the menu simply consist of years and months.

2025 Month Month Month entry for month goes here

2024 Month Month Month entry for month goes here

When users clicks on the year, the option to select months for that particular year comes up, then when user clicks on the month a md file will load underneath.

When user clicks on the month the function JAN() executes to load the blog entry.

function JAN() {
    let entry = document.querySelector(`#entry`)
    fetch(`/journal/entries/2025/JAN.md`)
    .then(async r => {
      if (r.ok) {
        mdparse(await r.text(), {"permissive": true, "section": true})
        .then(r => {
          entry.innerHTML = r
        })
      }
    })
  }

What I'm trying to figure out is how to write the JAN() function so it's reusable instead of having write same thing for each month. I was thinking about maybe having a variable inside the function that is dynamic and grabs name from a div id or something like that but not sure how to implement that.

What would be a simple solution for this? Thanks!

So I have a simple menu for a blog that's working but I'm trying to figure out if there's a way to reuse onClick function. So the menu simply consist of years and months.

2025 Month Month Month entry for month goes here

2024 Month Month Month entry for month goes here

When users clicks on the year, the option to select months for that particular year comes up, then when user clicks on the month a md file will load underneath.

When user clicks on the month the function JAN() executes to load the blog entry.

function JAN() {
    let entry = document.querySelector(`#entry`)
    fetch(`/journal/entries/2025/JAN.md`)
    .then(async r => {
      if (r.ok) {
        mdparse(await r.text(), {"permissive": true, "section": true})
        .then(r => {
          entry.innerHTML = r
        })
      }
    })
  }

What I'm trying to figure out is how to write the JAN() function so it's reusable instead of having write same thing for each month. I was thinking about maybe having a variable inside the function that is dynamic and grabs name from a div id or something like that but not sure how to implement that.

What would be a simple solution for this? Thanks!

Share Improve this question edited Feb 5 at 19:15 user29519291 asked Feb 5 at 19:10 user29519291user29519291 32 bronze badges 1
  • Put your functions in an object, then you can call them with dynamic object keys. – mykaf Commented Feb 5 at 19:12
Add a comment  | 

1 Answer 1

Reset to default 0

You can pass year and month as parameters to make your function dynamic like this:

function loadEntry(year, month) {
    let entry = document.querySelector('#entry');
    fetch(`/journal/entries/${year}/${month}.md`)
        .then(async r => {
            if (r.ok) {
                const content = await r.text();
                const parsedContent = await mdparse(content, {"permissive": true, "section": true});
                entry.innerHTML = parsedContent;
            }
        });
}

And call function like this:

<button onclick="loadEntry(2025, 'JAN')">January 2025</button>
<button onclick="loadEntry(2024, 'FEB')">February 2025</button>

Let me know if any confusions or need clarity.

发布评论

评论列表(0)

  1. 暂无评论