Satjeet Satjeet - 1 year ago 73
Javascript Question

Meteor - get the value from a custom attribute

I got a problem, I making a litte game where you come through stages, and in every stage you have to make a choose.
I want to save some of the information that the user pick, in "hola" and "chao", and later in the helpers save that info in sessions.
But I dont know how to get the value of "hola"---> {{fidelidadSegmento}}

<select class="form-control" id="select0" name="categ">
<option value = "0" selected="selected"> Stay </option>
{{#each op in opciones}}
<option value="{{op.costo}}" hola="{{fidelidadSegmento}}" chao="{{op.nombre}}"> {{fidelidadSegmento}} con {{op.nombre}} , eficiencia: {{op.eficiencia}}</option>costo

what I see in the browser is :

<select id="select0" class="form-control" name="categ">
<option selected="selected" value="0"> Stay </option>
<option value="3" hola="Awereness" chao="tv"> Awereness con tv , eficiencia: 0.8</option>

<option value="2" hola="Awereness" chao="magazine"> Awereness con magazine , eficiencia: 0.7</option>


And I want to get the value of the attribute "hola" and "chao"

console.log($(this).attr('hola')); get me undefined
and $("hola") get me [object Object]

Answer Source

If you are trying to use jQuery then try accessing the selected option using:

$("select option:selected")

But remember the default option does not have the attributes 'hola' or 'chao'. You will have to test for the attribute to be undefined or add the 'hola' and 'chao' attributes to the default option.

// This will return undefined for the default option
$("select option:selected").attr("hola"); 

This post might be helpful to you also.