/    Sign up×
Community /Pin to ProfileBookmark

Correct use of the <label> tag?

Aside from the ‘id’ values, ?
1. Is there any difference in the structure or execution of the following scripts?
2. Since they work the same, does one form have an advantage over the other?

[code]
<input type=”radio” name=”question1″ id=”q1a1″> <label for=”q1a1″>Answer 1</label><br>
<input type=”radio” name=”question1″ id=”q1a2″> <label for=”q1a2″>Answer 2</label><br>
<input type=”radio” name=”question1″ id=”q1a3″> <label for=”q1a3″>Answer 3</label><br>
<input type=”radio” name=”question1″ id=”q1a4″> <label for=”q1a4″>Answer 4</label><br>
<input type=”radio” name=”question1″ id=”q1a5″> <label for=”q1a5″>Answer 5</label>
[/code]

[code]
<label> <input type=”radio” name=”question1″> Answer 1 </label><br>
<label> <input type=”radio” name=”question1″> Answer 2 </label><br>
<label> <input type=”radio” name=”question1″> Answer 3 </label><br>
<label> <input type=”radio” name=”question1″> Answer 4 </label><br>
<label> <input type=”radio” name=”question1″> Answer 5 </label>
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@cootheadJan 17.2017 — Hi there JMRKER,

[indent]

my personal preference is keeping the elements separate.

I have two reasons for this assertion...

[list=1]
  • [*][color=navy] distinct styling advantages[/color]

  • [*][color=navy] it enables CSS toggling effects for type="radio" an type="checkbox" [/color]

  • [/list]


    [/indent]

    [i]coothead[/i]
    Copy linkTweet thisAlerts:
    @JMRKERauthorJan 18.2017 — Thanks 'coothead' and '.'.

    I was confused as to the benefits of one version over the other due to this tip.

    Tips and Notes

    Tip: A label can be bound to an element either by using the "for" attribute, or by placing the element inside the <label> element.
    [/quote]

    (From the 'w3schools' link above)

    I can visualize the benefits of the label being outside the checkbox element,

    but I don't see any for the radio button elements.

    The question was not for any special project, just to improve my understanding.

    Appreciate all inputs. ?
    Copy linkTweet thisAlerts:
    @cootheadJan 18.2017 — Hi there JMRKER,

    [indent]
    [color=navy]I can visualize the benefits of the label being outside the checkbox element,

    but I don't see any for the radio button elements. [/color]
    [/quote]


    As mentioned in my previous post...

    [color=navy]it enables CSS toggling effects for type="radio" an type="checkbox"[/color][/quote]

    If either of those two elements are confined within the label element,

    then the cascading effect of CSS nullified

    Here is an type="radio" example...

    [color=navy]
    &lt;!DOCTYPE html&gt;
    &lt;html lang="en"&gt;
    &lt;head&gt;

    &lt;meta charset="utf-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;

    &lt;title&gt;untitled document&lt;/title&gt;
    &lt;!--&lt;link rel="stylesheet" href="yourpage.css" media="screen"&gt;--&gt;
    &lt;style media="screen"&gt;
    body {
    font: 100%/150% arial,helvetica,sans-serif;
    background-color: #f0f0f0;
    }
    h1 {
    text-align: center;
    }
    #par {
    display: inline-block;
    padding: 0;
    margin: 0 0 1em;
    border: 1px solid #999;
    border-radius: 0.5em;
    list-style: none;
    background-color: #fff;
    }
    #par li {
    padding: 0.25em 0.5em;
    margin: 0.5em 0;
    }
    #par label {
    font-weight: bold;
    cursor: pointer;
    }
    #par label:hover {
    color: #c00;
    }
    #box {
    width: 90%; /* adjust to suit */
    margin: auto;
    border: 1px solid #999;
    border-radius: 1em;
    overflow: hidden;
    background-color: #fff;
    box-shadow: inset 0 0 1em rgba(0,0,0,0.4),
    0.25em 0.25em 0.25em rgba(0,0,0,0.4);
    }
    #box input {
    position: absolute;
    left: -999em;
    }
    #inner {
    width: 300%;
    margin: 0;
    transition: margin 1s ease-in-out;
    }
    #inner div {
    float: left;
    width: 33.333%;
    padding: 1.5%;
    box-sizing: border-box;
    font-size: 80%;
    }
    #inner h2 {
    text-align: center;
    }
    #inner h3 {
    text-align: right;
    }
    #inner h3 span {
    margin-left: 1em;
    font-weight: normal;
    }
    #r1:checked~#inner {
    margin-left: 0;
    }
    #r2:checked~#inner {
    margin-left: -100%;
    }
    #r3:checked~#inner {
    margin-left: -200%;
    }
    &lt;/style&gt;

    &lt;/head&gt;
    &lt;body&gt;
    &lt;h1&gt;CSS content changer&lt;/h1&gt;
    &lt;ul id="par"&gt;
    &lt;li&gt;&lt;label for="r1"&gt;Paragraph One&lt;/label&gt;&lt;/li&gt;
    &lt;li&gt;&lt;label for="r2"&gt;Paragraph Two&lt;/label&gt;&lt;/li&gt;
    &lt;li&gt;&lt;label for="r3"&gt;Paragraph Three&lt;/label&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;div id="box"&gt;
    &lt;input id="r1" name="r" type="radio"&gt;
    &lt;input id="r2" name="r" type="radio"&gt;
    &lt;input id="r3" name="r" type="radio"&gt;

    &lt;div id="inner"&gt;
    &lt;div&gt;
    &lt;h2&gt;Lorem ipsum&lt;/h2&gt;
    &lt;p&gt;
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sit amet sem sed libero
    bibendum tempus faucibus quis mi. Nulla rhoncus vel ipsum in volutpat. Nam tortor nibh,
    posuere ac lorem ut, suscipit tincidunt leo. Duis interdum justo ac justo vehicula consequat.
    Curabitur et volutpat nibh. Phasellus rutrum lacus at dolor placerat feugiat. Morbi porta,
    sapien nec molestie molestie, odio magna vestibulum lorem, vitae feugiat est leo sit amet
    nunc. Curabitur ornare tempor turpis. In nibh sem, porta ac magna sed, pretium commodo
    odio. Sed porttitor augue velit, quis placerat diam sodales ac.
    &lt;/p&gt;
    &lt;/div&gt;&lt;div&gt;
    &lt;h2&gt;Donec vehicula&lt;/h2&gt;
    &lt;p&gt;
    Donec vehicula diam non leo efficitur, id euismod odio tincidunt. Vivamus auctor viverra
    purus vitae lobortis. Sed et libero non diam tempor sagittis. Quisque vel egestas ipsum.
    Integer sed elit eu orci blandit commodo in in erat. Donec blandit, mi at gravida varius,
    nulla tellus vulputate ex, vitae euismod erat lectus rutrum diam. *** sociis natoque
    penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur tristique
    varius ullamcorper.
    &lt;/p&gt;
    &lt;/div&gt;&lt;div&gt;
    &lt;h2&gt;Nam venenatis diam&lt;/h2&gt;
    &lt;p&gt;
    Nam venenatis diam ante, et cursus elit porttitor at. Morbi mattis leo at ex vehicula, non
    vestibulum ligula aliquam. Maecenas non nibh sollicitudin, porttitor odio in, elementum arcu.
    Donec pulvinar orci enim. In pulvinar congue ante, ac rutrum odio bibendum volutpat.
    Phasellus ac sem consequat lorem congue malesuada vitae vitae diam. Donec eu imperdiet
    augue. Curabitur ullamcorper sit amet libero in ullamcorper. Donec sed laoreet quam.
    Phasellus malesuada libero felis, non elementum ex tincidunt eget. Quisque cursus arcu vel
    diam consectetur, ac imperdiet est cursus. Fusce id nunc nibh.
    &lt;/p&gt;
    &lt;h3&gt;Some Author &lt;span&gt; c. 1260 – c. 1327&lt;/span&gt;&lt;/h3&gt;
    &lt;/div&gt;
    &lt;!-- #inner --&gt;&lt;/div&gt;
    &lt;!-- #box --&gt;&lt;/div&gt;

    &lt;/body&gt;
    &lt;/html&gt;[/color]


    [/indent]

    [i]coothead[/i]
    Copy linkTweet thisAlerts:
    @JMRKERauthorJan 18.2017 — Thank you 'coothead'. ?

    I would not have considered creating that as a solution.

    I learned quite a bit from your concrete example,

    especially some of the css conditions used with the radio buttons.
    Copy linkTweet thisAlerts:
    @cootheadJan 18.2017 — Hi there JMRKER,

    [indent]

    Since CSS3, I always look for alternatives for javascript methods.

    [/indent]

    [i]coothead[/i]
    ×

    Success!

    Help @JMRKER spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...