Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Radio buttons should not have ID attributes, but they take the ID of the first <input> tag #48

Open
petdance opened this issue Mar 14, 2024 · 0 comments

Comments

@petdance
Copy link

Consider the following:

use HTML::Form;
use Data::Dumper;
$Data::Dumper::Indent = $Data::Dumper::Terse = 1;

my ($form) = HTML::Form->parse( <<'HTML', 'https://example.com' );
<form>
    <select id="id-language" name="language">
        <option id="id-english" value="English"> English </option>
        <option id="id-klingon" value="Klingon" selected> Klingon </option>
    </select>

    <input class="green" id="id-happy" name="mood" value="happy" type="radio" checked>
    <input class="red"   id="id-sad"   name="mood" value="sad"   type="radio">
</form>
HTML

for my $id ( qw( id-language id-klingon id-happy id-sad ) ) {
    my $input = $form->find_input( "#$id" );
    say "$id is ", Dumper( $input );
}

Results are:

id-language is bless( {
  'type' => 'option',
  'option_id' => 'id-english',
  'current' => 1,
  'idx' => 1,
  'id' => 'id-language',
  'name' => 'language',
  'menu' => [
    {
      'value' => 'English',
      'name' => 'English'
    },
    {
      'value' => 'Klingon',
      'seen' => 1,
      'name' => 'Klingon'
    }
  ]
}, 'HTML::Form::ListInput' )

id-klingon is undef

id-happy is bless( {
  'menu' => [
    {
      'name' => '',
      'seen' => 1,
      'value' => 'happy'
    },
    {
      'name' => '',
      'value' => 'sad'
    }
  ],
  'class' => 'green',
  'current' => 0,
  'type' => 'radio',
  'name' => 'mood',
  'id' => 'id-happy'
}, 'HTML::Form::ListInput' )

id-sad is undef

This demonstrates that:

Good: If a HTML::Form::ListInput is created from a <select> tag, its ID attribute is taken from the id attribute of the <select> and the ids in the <option>s are ignored.

Less good: for radio buttons, there is no enclosing tag around the <input>s, and HTML::Form takes the ID attribute from the first <input> tag and applies it to the whole HTML::Form::ListInput object. I don't think that makes sense. It seems to me that an HTML::Form::ListInput from a set of radio button inputs should have no ID at all. If this IS intended behavior, then I think that's something that should be explained in the docs.

@petdance petdance changed the title Radio buttons should not have ID attributes, but take the ID of the first <input> tag Radio buttons should not have ID attributes, but they take the ID of the first <input> tag Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant