Lesson 14 of 60 – JSX Attributes
23%

JSX Attributes

JSX attributes are used to provide additional information to React elements. They look similar to HTML attributes, but JSX follows JavaScript and React naming rules.

Note: JSX attributes can contain strings, JavaScript expressions, variables, objects, boolean values, and other dynamic data.

1. What Are JSX Attributes?

Attributes provide additional information about an element.

<img src="photo.jpg" alt="Student" />

Here, src and alt are attributes.

2. JSX Attributes Look Like HTML Attributes

Many JSX attributes look familiar to HTML developers.

<input
    type="text"
    placeholder="Enter your name"
/>

The syntax is similar to HTML, but JSX has additional rules.

3. String Attribute Values

String values can be written inside quotation marks.

<h1 className="title">
    React Tutorial
</h1>

The value "title" is a string.

4. className Attribute

In JSX, use className instead of the HTML class attribute.

<div className="container">
    <h1>Welcome</h1>
</div>

5. htmlFor Attribute

For a label element, JSX uses htmlFor instead of for.

<label htmlFor="email">
    Email
</label>

<input id="email" type="email" />

6. Dynamic Attribute Values

JavaScript variables can be used as attribute values with curly braces.

const imageUrl = "photo.jpg";

return (
    <img
        src={imageUrl}
        alt="Profile"
    />
);

7. Using Variables in Attributes

const title = "React Course";

return (
    <h1 title={title}>
        Learn React
    </h1>
);

The value of the title variable is assigned to the attribute.

8. src Attribute

The src attribute specifies the source of an image or other resource.

<img
    src="logo.png"
    alt="Company Logo"
/>

9. alt Attribute

The alt attribute provides alternative text for an image.

<img
    src="student.jpg"
    alt="Student Photo"
/>

Meaningful alternative text can improve accessibility.

10. href Attribute

The href attribute specifies the destination of a link.

<a href="https://example.com">
    Visit Website
</a>

11. target Attribute

The target attribute controls where a link is opened.

<a
    href="https://example.com"
    target="_blank"
>
    Open Website
</a>

12. id Attribute

The id attribute gives an element a unique identifier.

<div id="main-content">
    React Tutorial
</div>

13. title Attribute

The title attribute can provide additional information about an element.

<button title="Save your work">
    Save
</button>

14. Boolean Attributes

Some JSX attributes represent boolean values.

<button disabled>
    Submit
</button>

The disabled attribute represents a true value when present.

15. Dynamic Boolean Attributes

A JavaScript boolean can be assigned to an attribute using curly braces.

const isDisabled = true;

return (
    <button disabled={isDisabled}>
        Submit
    </button>
);

16. checked Attribute

The checked attribute can control the checked state of a checkbox when used appropriately with React state or props.

const selected = true;

return (
    <input
        type="checkbox"
        checked={selected}
        readOnly
    />
);

17. value Attribute

The value attribute can be used with form elements.

const name = "Rahul";

return (
    <input
        type="text"
        value={name}
        readOnly
    />
);

18. placeholder Attribute

The placeholder attribute displays a hint inside an empty input.

<input
    type="text"
    placeholder="Enter your name"
/>

19. onClick Attribute

React event attributes use camelCase names such as onClick.

function handleClick() {
    alert("Button clicked!");
}

return (
    <button onClick={handleClick}>
        Click Me
    </button>
);

20. onChange Attribute

The onChange attribute is commonly used with form controls.

function handleChange(event) {
    console.log(event.target.value);
}

return (
    <input
        type="text"
        onChange={handleChange}
    />
);

21. Style Attribute

The style attribute can receive a JavaScript object.

const style = {
    color: "blue",
    fontSize: "24px"
};

return (
    <h1 style={style}>
        React
    </h1>
);

22. Inline Style Object

A style object can also be written directly inside JSX.

<p
    style={{
        color: "green",
        fontSize: "20px"
    }}
>
    Learn React
</p>

23. Dynamic className

A JavaScript variable can dynamically determine the CSS class.

const statusClass = "active";

return (
    <div className={statusClass}>
        Active Student
    </div>
);

24. Multiple Attributes

An element can contain multiple attributes.

<img
    src="profile.jpg"
    alt="Student"
    className="profile-image"
    title="Student Profile"
/>

25. Attribute Expressions

JavaScript expressions can be used directly as attribute values.

const width = 200;

return (
    <img
        src="photo.jpg"
        alt="Photo"
        width={width}
    />
);

26. Spread Attributes

The spread syntax can be used to pass multiple properties to a JSX element.

const props = {
    className: "card",
    title: "Student Card"
};

return (
    <div {...props}>
        Student Information
    </div>
);

The properties inside props are passed to the element.

27. Data Attributes

Custom data attributes can be written using the data- naming pattern.

<button data-id="101">
    Student
</button>

Data attributes can store custom data associated with an element.

28. Accessibility Attributes

Accessibility-related attributes can be used in JSX. Some ARIA attributes use the aria- naming pattern.

<button
    aria-label="Close menu"
>
    X
</button>

Accessibility attributes help communicate information about the UI to assistive technologies.

29. JSX Attribute Best Practices

  • Use className instead of class.
  • Use htmlFor instead of for for labels.
  • Use camelCase for React event attributes.
  • Use curly braces for JavaScript values.
  • Use meaningful alt text for images.
  • Use boolean attributes correctly.
  • Keep long attribute lists formatted clearly.
  • Avoid unnecessary inline styles when reusable CSS is more suitable.
  • Use accessibility attributes where appropriate.

30. Complete JSX Attributes Example

function StudentCard() {

    const studentName = "Rahul";
    const imageUrl = "student.jpg";
    const isActive = true;

    function handleClick() {
        alert("Student: " + studentName);
    }

    return (
        <div
            className="student-card"
            title="Student Information"
        >

            <img
                src={imageUrl}
                alt="Student Profile"
                className="profile-image"
            />

            <h2>
                {studentName}
            </h2>

            <p className={isActive ? "active" : "inactive"}>
                {isActive ? "Active Student" : "Inactive Student"}
            </p>

            <button
                onClick={handleClick}
                disabled={!isActive}
            >
                View Profile
            </button>

        </div>
    );

}

export default StudentCard;

This example combines static attributes, dynamic attributes, expressions, className, event handling, boolean attributes, and conditional values.

📌 Key Points

  • JSX attributes provide additional information to React elements.
  • Many JSX attributes look similar to HTML attributes.
  • Use className instead of class.
  • Use htmlFor instead of for for labels.
  • Use curly braces to provide JavaScript values to attributes.
  • Variables can be used as dynamic attribute values.
  • Boolean attributes can receive true or false values.
  • React event attributes such as onClick use camelCase.
  • The style attribute can receive a JavaScript object.
  • Multiple attributes can be used on the same JSX element.
  • Spread syntax can pass multiple properties to an element.
  • Accessibility attributes such as aria-label can be used in JSX.

🧠 Quick Quiz

Question: Which attribute is normally used instead of the HTML class attribute in JSX?