Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6699265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:40:29+00:00 2026-05-26T06:40:29+00:00

I have an HTML 5 page with several articles. Each article has a custom

  • 0

I have an HTML 5 page with several articles. Each article has a custom attribute titled “MyAttribute”:

<html>
    <article MyAttribute="x">
    <article MyAttribute="y">
    <article MyAttribute="z">
</html>

I would like to create a function that does something like the following pseudo-code:

for each article {
    get MyAttribute from the article
    if MyAttribute == x {do this}
    if MyAttribute == y {do that}
    if MyAttribute == z {do something else}
}

What is the most efficient way to do this using jQuery/JavaScript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:40:30+00:00Added an answer on May 26, 2026 at 6:40 am

    Instead of using custom attributes, I’d recommend placing your “MyAttribute” in a data-* attribute like this:

    <html>
      <body>
        <article data-myattribute="x"></article>
        <article data-myattribute="y"></article>
        <article data-myattribute="z"></article>
      </body>
    </html>
    

    This creates “more valid” HTML. Then you would write javascript like this:

    $('article').each(function (){
        switch ($(this).data('myattribute')){
            case 'x':
                //do something for x
                alert('this is case X');
                break;
            case 'y':
                //do something for y
                alert('this is case Y');
                break;
            case 'z':
                //do something for z
                alert('this is case Z');
                break;
        }
     });
    

    I’ve also created a jsFiddle of the result for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML page with several iframes, each pointing to a text file
I have heavy HTML-page with several megabytes of images and scripts. I'd like to
I have a C# webbrowser that holds an html page that has several iframes.
Here I have html structure. It repeats several times on the page. <article class=boo>
I have several elements on the html page, I call them through javascript like
I have HTML page with several frames on it. One of frames(called Main) contains
I have several forms in one HTML page... I have to filter these forms
I've got several elements on a HTML page which have the same class -
Assume that I have some HTML page, and a corresponding CSS file. I'd like
I have in an HTML page, an element that appears several times, and running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.