JavaScript Adding title attribute to select option dynamically with .append() adding title attribute using jquery

Javascript, Jquery, HTML

I am adding select options to a select box dynamically. I take each unique element in an array and add it as an option to the select element. It works great, but I need to add a title attribute at the same time, with the same value as the option text. The end goal of this is to make tooltips for each option.

So instead of <option>value</option>, it looks like

<option title="value">value</option>

Does that make sense?

Current HTML:

<select id="Process_Issue" class="fieldLabel2 IncidentInputField dynamicFields1"></select>

JS:

$.each(eliminateDuplicates(aryProcess), function (key, value) { $('#Process_Issue').append($("<option/>", { text: cleanNulls(value) })); });
Answer:1

You can just specify the title upon appending:

JSFiddle

HTML

<select id="my_select"></select>

JS

$('#my_select').append('<option title="value1">value1</option>');
$('#my_select').append('<option title="value2">value2</option>');
$('#my_select').append('<option title="value3">value3</option>');
Answer:2

You can set the title attribute

$('#Process_Issue').append(
    $("<option/>", { text: value }).attr("title",value)
 ); 

Here is a working sample http://jsbin.com/ozudoTod/1/

Answer:3

You seem to be using the same selector multiple times for each iteration in the array. Instead cache it and save some lookup time.

var $select = $('#Process_Issue');
$.each(eliminateDuplicates(aryProcess), function (key, value) { 
     var val = cleanNulls(value);
     $select .append($("<option/>", { 
          text: val,
          title: val
     })); 
});

If this does not work use .attr method to hook up the attribute to the element.

var $select = $('#Process_Issue');
$.each(eliminateDuplicates(aryProcess), function (key, value) { 
    var val = cleanNulls(value);
    $('<option/>').attr({
         text: val,
         title: val
    }).appendTo($select);
});
Answer:4

I'm trying to create web-page, and to improve performance, I decide to use event delegation, instead of direct binding, but I came across with strange behavior of event delegation or I missed ...

I'm trying to create web-page, and to improve performance, I decide to use event delegation, instead of direct binding, but I came across with strange behavior of event delegation or I missed ...

  1. javascript event delegation example
  2. javascript event delegation and bubbling
  3. javascript event delegation without jquery
  4. javascript event delegation model
  5. javascript event delegation mdn
  6. javascript event delegation performance
  7. javascript event delegation
  8. javascript event delegation library
  9. javascript event delegation pattern
  10. javascript event delegation jquery
  11. javascript event delegation click
  12. javascript event delegation tutorial
  13. javascript event delegation mouseover
  14. javascript event delegation focus
  15. javascript event delegation use
  16. javascript event-delegation contains
  17. javascript delegation event.target
  18. vanilla javascript event delegation
  19. explain javascript event delegation model
  20. pure javascript event delegation

I am running the Native messaging sample app. (http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/nativeMessaging/). I have added registry key ...

I am running the Native messaging sample app. (http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/nativeMessaging/). I have added registry key ...

  1. chrome native messaging example
  2. chrome native messaging host
  3. chrome native messaging c#
  4. chrome native messaging tutorial
  5. chrome native messaging whitelist
  6. chrome native messaging security
  7. chrome native messaging python
  8. chrome native messaging not running uipath
  9. chrome native messaging
  10. chrome native messaging c# example
  11. chrome native messaging.exe not running
  12. chrome native messaging java
  13. chrome native messaging.exe
  14. chrome native messaging not working
  15. chrome native messaging debug
  16. chrome native messaging manifest
  17. chrome native messaging disconnect
  18. chrome native messaging host example
  19. chrome native messaging host c#
  20. chrome native messaging allowed_origins

I am creating a site using sails and passport for authentication purposes. I've got problems when it come to the use of Jquery and backbone in my code though. It seems that both are down when i tried ...

I am creating a site using sails and passport for authentication purposes. I've got problems when it come to the use of Jquery and backbone in my code though. It seems that both are down when i tried ...

  1. sails backbone

In my HTML file, I want to set a check box with specific name as well as id as= checked. How can I acheive this..? eg: <input type="checkbox" name="myName_1" id="1" value="my Value 1"> my ...

In my HTML file, I want to set a check box with specific name as well as id as= checked. How can I acheive this..? eg: <input type="checkbox" name="myName_1" id="1" value="my Value 1"> my ...

  1. check checkbox with specific value
  2. check specific port windows
  3. check specific port linux
  4. check specific port
  5. check specific port status windows
  6. check specific port open windows
  7. check specific word in string php
  8. check specific commit git
  9. check specific gravity of battery
  10. check specific port cmd
  11. check specific character in string php
  12. check specific character in string javascript
  13. check specific port status linux
  14. check specific port netstat
  15. check specific information
  16. check specific open ports cmd
  17. check specific dns server
  18. check specific port nmap
  19. check specific port open linux
  20. check specific port ubuntu