0

i am new in javascript and jquery, i tried many things that is found in site like related questions but i could not solve my problem.

How can i take a value from this piece of html ?

<div class="frmPillIdentifier-field-fmt">
 <select id="scolor" name="scolor" size="1">
  <option id="colorOption" value="8">Kirli Beyaz</option>
 </select>

How can get a value in option tag?

i tried something like this but it didnt solve my problem :

$colorValue = jQuery('#frmPillIdentifier-field-fmt > select[name=scolor] > option[id=colorOption]');
//$colorValue = $('#colorOption').val();
  $shapeValue = $('#shapeOption').val();
  console.log("Renkler"+$colorValue.val()+$shapeValue);

Console Error is like this " = Renklerundefinedundefined "

Gerlav
  • 53
  • 2
  • 8
  • Do you want the value of the option that's currently selected, or the value of that specific option, no matter what is actually selected? – JJJ May 17 '14 at 11:07
  • I tried it but it doesnt solve my problem – Gerlav May 17 '14 at 11:07
  • I want just to get vale inside – Gerlav May 17 '14 at 11:08
  • 1
    Is your code inside `$(document).ready()`? – Barmar May 17 '14 at 11:09
  • Start by opening your console (F12) and check for errors. – adeneo May 17 '14 at 11:14
  • I have spotted a mistake in the code: #frmPillIdentifier-field-fmt instead of .frmPillIdentifier-field-fmt for a class. Either you need to change the class to id in the div tag or you need to change it to a dot instead of hash in jquery. However, this correction still may not fix the issue. – Krishna Sapkota May 17 '14 at 11:15
  • Just curious, but why do you use `$` in your variable names? This is javascript.. – aIKid May 17 '14 at 11:15
  • @alKid It's common to use $ in variables that hold jQuery objects (although `$shapeValue` does hold a string). – JJJ May 17 '14 at 11:58

5 Answers5

1

You can use for getting value;

$("#colorOption").attr("value");
umut
  • 1,016
  • 1
  • 12
  • 25
1

Change the first line to:

$colorValue = jQuery('.frmPillIdentifier-field-fmt > select[name=scolor] > option[id=colorOption]');

frmPillIdentifier-field-fmt is a class, not an ID, so you use . to select it.

DEMO

Or you can simply use:

$colorValue = $("#colorOption");

Since IDs are unique, you don't have to give a detailed path to access it.

Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

use this

select = document.getElementById("scolor");
shapeValue=select.value;
ashishmaurya
  • 1,196
  • 10
  • 18
-1

Enough of this:

    <select id="scolor" name="scolor" size="1">
  <option value="_0">--Choose--</option> 
  <option value="_8">Kirli Beyaz</option>
  <option value="_9">Some thing</option>
</select>    


var colorSet = {
 _0: 'data0'
 ,_8: 'data8'
 ,_9: 'data9'
}


var shapeValue = $('#shapeOption').val();
var colorValue = calorSet[ shapeValue]

console.log("Renkler" + colorValue + shapeValue);

PS You can not duplicate IDs. And without all the variables Var be in the global scope that bad.

Alex
  • 41
  • 3
-1

Try to Use this code:

$('#scolor').change(function(){
    var optionvalue = $("#colorOption option:selected").text();
});
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
laxman
  • 1
  • 1
  • 4