skip to Main Content

I’m struggling to figure out the following;

I have a django form field hidden in {{ field.field }}. It renders out as

<input type="text" name="field-id" value="100" id="id_set-0-product" class="vForeignKeyRawIdAdminField">

I can tagret it using jQuery by its id but I need to add a list = "choicelist" which corresponds with

<datalist id="choicelist" style="text-align: center">
                            </datalist>

which I populate using ajax requests. How do I add the list = "choicelist" to it? I am basically looking for something like .addClass() but for a list.

Any help would be much appreciated!

3

Answers


  1. You have to use the attr() method for this:

     $('#id_set-0-product').attr('list', 'choicelist')
    
    Login or Signup to reply.
  2. You have two options

    Using ID (it will add list attribute for particular field)

      $('#id_set-0-product').attr('list', 'choicelist')
    

    Using Class (it will add list attribute for all available field with the same class)

    $('.vForeignKeyRawIdAdminField').attr('list', 'choicelist')
    
    Login or Signup to reply.
  3. Using Django, add the attribute to the widget:

    attrs

    A dictionary containing HTML attributes to be set on the rendered widget.

        >>> from django import forms
        >>> name = forms.TextInput(attrs={'size': 10, 'title': 'Your name'})
        >>> name.render('name', 'A name')
        '<input title="Your name" type="text" name="name" value="A name" size="10">'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search