0

I'm trying to insert an additional select option to a list, but with specific data elements:

function myFunction() {
    var x = document.getElementById("mySelectList");
    var option = document.createElement('option', {
        "data-img-src": "https://blah",
        "data-img-alt": "https://blah"
    });
    option.text = "New Image";
    x.add(option);
}

This is intended to allow image-picker to detect the additional item and hopefully display it.

But the additional attributes are ignored. I'd really appreciate some help please, as I'm going out of my mind. Thanks

0

3 Answers 3

1

I would add that x.add is not valid, use appendChild instead : Node.appendChild

To sum up :

function myFunction() {
  var x = document.getElementById("mySelectList");
  var option = document.createElement('option');
  option.text = "New Image";
  option.setAttribute("data-img-src", "https://blah");
  option.setAttribute("data-img-alt", "https://blah");
  x.appendChild(option);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes - quite right.. thanks. its now working using this above code.. thanks very much for the help. Is it JQuery thats meaning that the x.appendChild(option) works in my real code, but the x.add(option) worked on a more basic example?
0

use setAttribute :

function myFunction() {
  var x = document.getElementById("mySelectList");
  var option = document.createElement('option');
  option.text = "New Image";
  option.setAttribute("data-img-src", "https://blah");
  option.setAttribute("data-img-alt", "https://blah");
  x.add(option);
}

myFunction()
<select id="mySelectList">

</select>

1 Comment

Thanks very much. That's perfect.
0

The second argument you are using isn't valid for createElement()

options
An optional ElementCreationOptions object containing a single property named is, whose value is the tag name for a custom element previously defined using customElements.define(). See Web component example for more details.

Use setAttribute() or dataset["name"] to set data-* attributes

option.setAttribute("data-img-src", "https://blah");
option.setAttribute("data-img-alt", "https://blah");

//note dataset uses camelcase for dashed names
option.dataset["imgSrc"] = "https://blah";
option.dataset["imgAlt"] = "https://blah";

function myFunction() {
  var x = document.getElementById("mySelectList");
  var option = document.createElement('option');
  option.text = "New Image";
  option.setAttribute("data-img-src","https://blah");
  option.setAttribute("data-img-alt","https://blah");
  x.add(option);
  
  //just for displaying what the attributes were set to
  document.body.insertAdjacentHTML('beforeend',`<br>src: ${option.dataset.imgSrc}`);
}

document.querySelector('button').addEventListener('click',myFunction);
<select id="mySelectList"></select>
<button>Add</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.