0

I have this small function:

$('#part[en]').keyup(function(){
    //console.log($(this).val());
    $slug = slugify($(this).val());
    $('#part_slug').val($slug);
})

It doesn't work. After some try and error, I understood the problem is with the ID (part[en]). If I change it to "part" (and change it in HTML too) it works. My problem is that I need to use part[en] since I need to work them as an array with PHP.

How can I achieve this? Also, I need to do the exact same thing with 2 ID's (keyup or change). There's any way to make something link

part[en].keyup OR part2[en].keyup

Or I need to create two seperate functions? Thanks in advance.

StudioTime
  • 22,603
  • 38
  • 120
  • 207
Tiago
  • 625
  • 5
  • 16
  • what is `[en]` in your id? – null Sep 18 '18 at 15:06
  • It's the array created. I have some inputs each one in their own language (like part[en], part[es]. Then I work them as an array, for each [xx] in part. – Tiago Sep 18 '18 at 15:07
  • 2
    IDs are meant to be unique, if you want to associate multiple elements, use classes instead of IDs. – Titus Sep 18 '18 at 15:07
  • and the ID is unique. part[en] is different from part[es] . My problem is that JS seems not to accept the [en] part. And I need to validate that... :\ – Tiago Sep 18 '18 at 15:08
  • 2
    Possible duplicate of [Need to escape a special character in a jQuery selector string](https://stackoverflow.com/questions/2786538/need-to-escape-a-special-character-in-a-jquery-selector-string) – 31piy Sep 18 '18 at 15:09
  • The problem with the code is that `[]` are special tokens to trigger attribute selector. You need to escape those. – 31piy Sep 18 '18 at 15:10
  • I see, in that case, use camel case notation `partEn` or dashes `part_en` because `[` and `]` are not accepted characters for IDs. – Titus Sep 18 '18 at 15:10
  • 1
    You don't need array notation to set the same function as a callback for an event for multiple elements, you can specify a selector for multiple elements (eg: `$("part_en, part_es")`). – Titus Sep 18 '18 at 15:15

0 Answers0