JavaScript code for getting the selected value from a combo box - javascript

Can any one give me a sample code that gets the selected value from an existing combo box?
I have this code but its not doing anything:
function check ()
{
var e = document.getElementById("ticket_category_clone");
var str = e.options[e.selectedIndex].text;
alert(str);
if (str==="Hardware")
{
SPICEWORKS.utils.addStyle('#ticket_c_hardware_clone{display: none !important;}');
}
}
SPICEWORKS.app.helpdesk.ready(check);​
heres a img of the code
and the code
<select id="ticket_category_clone" name="ticket[category]" hdpp="ticket_category">
<option value=""></option><option value="Hardware">Hardware</option>
<option value="Software">Software</option>
<option value="Rede" selected="selected">Rede</option>
<option value="Pedidos">Pedidos</option>
<option value="Formação/Dúvida">Formação/Dúvida</option>
<option value="Outro">Outro</option><option value="#edit_categories#">Edit Categories...</option></select>
what i want its find a way to get the selected value fo that combobox

There is an unnecessary hashtag; change the code to this:
var e = document.getElementById("ticket_category_clone").value;

I use this
var e = document.getElementById('ticket_category_clone').value;
Notice that you don't need the '#' character in javascript.
function check () {
var str = document.getElementById('ticket_category_clone').value;
if (str==="Hardware")
{
SPICEWORKS.utils.addStyle('#ticket_c_hardware_clone{display: none !important;}');
}
}
SPICEWORKS.app.helpdesk.ready(check);​

It probably is the # sign like tho others have mentioned because this appears to work just fine.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<select id="#ticket_category_clone">
<option value="hw">Hardware</option>
<option>fsdf</option>
<option>sfsd</option>
<option>sdfs</option>
</select>
<script type="text/javascript">
(function check() {
var e = document.getElementById("#ticket_category_clone");
var str = e.options[e.selectedIndex].text;
alert(str);
if (str === "Hardware") {
alert('Hi');
}
})();
</script>
</body>

Related

Getting result out of a function into a usable variable?

So I have a simple HTML file that I want to have a select option drop down menu. I have made that when a selection is made the page reloads retaining the option but I can't get the result outside of the function.
<!doctype html>
<html>
<head>
</head><body>
<select id="mySelect" onchange = "getSelectValue();">
<optgroup label="Working">
<option value="rage.zip">Streets of Rage</option>
<option value="quest.zip">Money Quest</option>
</optgroup>
</select>
<div style="width:640px;height:480px;max-width:100%">
<script type="text/javascript">
window.addEventListener('load', function(){
if (localStorage.pick) {
var sel = document.querySelector('#mySelect');
sel.value = localStorage.pick;
}
});
function getSelectValue(){
var sel = document.querySelector('#mySelect');
localStorage.pick = sel.value;
location.reload();
return sel.value;
}
EJS_player = '#game';
EJS_gameUrl = ; // Url to Game rom
EJS_gameID = 4; // ID in your website, required for netplay.
EJS_core = 'segaMD';
</script>
<script src="https://www.emulatorjs.com/loader.js"></script>
</body>
</html>
My issue is the select option result is in the sel.value. I need that value to EJS_gameUrl = .
So if the option 1 is select which is rage.zip then it should be filled in the EJS_gameUrl = "rage.zip"
Im just having trouble with getting that result out of the function . I would prefer to define it to a variable and just add the variable in the EJS_gameUrl = x; or similar. How can I go about this?
See Storing the information you need — Variables, Storage.setItem and Storage.getItem.
Your code should be something along the lines of:
const EJS_player = '#game';
let EJS_gameUrl = ''; // Url to Game rom
const EJS_gameID = 4; // ID in your website, required for netplay.
const EJS_core = 'segaMD';
window.addEventListener('load', function() {
const storedValue = localStorage.getItem('mySelect');
if (storedValue) {
var sel = document.querySelector('#mySelect');
sel.value = storedValue;
EJS_gameUrl = storedValue;
}
});
function getSelectValue() {
localStorage.setItem('mySelect', this.value);
location.reload();
}
console.log(EJS_gameUrl)
<select id="mySelect" onchange="getSelectValue();">
<optgroup label="Working">
<option value="rage.zip">Streets of Rage</option>
<option value="quest.zip">Money Quest</option>
</optgroup>
</select>

how to pass dropdown selected value in jquery plugin

I need to pass the dropdown selected value to the jQuery plugin.
HTML:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.requiredplugin.js"></script>
</head>
<body>
<select name="Type" id="sid">
<option value="value1">value1</option>
<option value="value2" selected>value2</option>
<option value="value3">value3</option>
</select>
<textarea name="source" id="src1" cols="150" rows="20"></textarea>
<script type="text/javascript">
$("#src1").keyup(function(e) {
$(this).pluginmethod(e.keycode);
});
</script>
</body>
</html>
jquery.requiredplugin.js:
(function ($) {
// jQuery plugin definition
$.fn.pluginmethod = function (keycode) {
return this.each(function () {
var $str = $('#src1');
var $soml = $('#sid');
var somlan = $soml.val(); //if I assign var somlan with some static value my code works
var som = $str.val();
/* My code goes here where I use both var som and var somlan*/
});
};
})(jQuery);
I am able to get the value of $('#src1') and if assign var somlan with some static value then my code works. But if I want to use dropdown selected value(whose id is "sid" ) and retrieve the value in the plugin as var $soml = $('#sid'); then my code doesn't work.
How can I use dropdown selected value in my plugin?
Try this : modify your plugin method with two argument instead of one and pass value of select box as second argument.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.requiredplugin.js"></script>
</head>
<body>
<select name="Type" id="sid">
<option value="value1">value1</option>
<option value="value2" selected>value2</option>
<option value="value3">value3</option>
</select>
<textarea name="source" id="src1" cols="150" rows="20"></textarea>
<script type="text/javascript">
$("#src1").keyup(function(e) {
$(this).pluginmethod(e.keycode,$('#sid').val());
});
</script>
</body>
</html>
Plugin
(function($) {
// jQuery plugin definition
$.fn.pluginmethod = function(keycode, selectVal) {
return this.each(function() {
var $str = $('#src1');
//var $soml = $('#sid');
var somlan= selectVal;
var som= $str.val();
/* My code goes here where I use both var som and var somlan*/
});
};
})(jQuery);
try it like this in your jquery.requiredplugin.js
(function($) {
$.fn.pluginmethod = function(keycode) {
return this.each(function() {
var $str = $('#src1');
var $soml = $('#sid option:selected').text();
var somlan= $soml.val();
var som= $str.val();
/* Your code goes here where you use both var som and var somlan*/
});
};
})(jQuery);
Hope this helps.
You should try :
$("#sid option:selected").text();
or
$("#sid option").is("selected").text()
It looks like sormeone already asked a similar question at jQuery Get Selected Option From Dropdown
This will return an object if i'm right
var sid = $('#sid').find(":selected");

Searching and selecting an option value in a HTML select element

I need your help.
I am a newbie to javascript and I am unsure as to how I would go about searching for a specified option in a select box and then selecting it
HTML:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<select id="select1">
<option value="apples">apples</option>
<option value="oranges">oranges</option>
<option value="bananas">bananas</option>
<option value="mangos">mangos</option>
<option value="pears">pears</option>
</select>
</body>
</html>
ie.
lookup("select1","mangos")
javascript logic:
function lookup("selectid","stringtosearchfor") {
look through the selected "selectid" and find the string "mangos"
if found { select mangos from the select box }
}
How do you code that ?
Thanks in advance,
This is easier than you think... try:
document.getElementById('select1').value = 'mangos';
function lookup(id, value)
{
var ret = undefined;
if(document.getElementById(id) != undefined)
{
var options = document.getElementById(id).childNodes;
for(i = 0; i < options.length; i++)
{
if(options[i].value == value)
{
ret = options[i];
break;
}
}
}
return ret;
}
Via Jquery:
$('#select1').val('mangos');

How to get different selected values in javascript?

Hello everyone how can I get two values from different select boxes? I get first value in my select box but I can't get the second value from another select box.Here is my javascript code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var select = document.forms[0].sel;
var select2=document.forms[0].sel2;
select.onchange = function () {
var value = select.options[select.selectedIndex].value; // to get Value
alert(value);
}
select2.onchange = function () {
var value2 = select2.options[select2.selectedIndex].value; // to get Value
alert(value2);
}
});
</script>
<form>
<SELECT NAME="sel" onChange="split(selected value)">
<OPTION VALUE=1>Milk</option>
<OPTION VALUE=2>tea</option>
<OPTION VALUE=3>water</option>
<OPTION VALUE=4>coffee</option>
</SELECT>
</form>
<form>
<SELECT NAME="sel2" onChange="split(selected value)">
<OPTION VALUE=1>Milk</option>
<OPTION VALUE=2>tea</option>
<OPTION VALUE=3>water</option>
<OPTION VALUE=4>coffee</option>
</SELECT>
</form>
The second select box is in the second form. So:
var select2 = document.forms[1].sel2;
That said, you can actually use jQuery for these things:
// bind a change event handler to the second select
$("select").eq(1).on("change", function() {
alert( $(this).val() );
});
jQuery Tip - Getting Select List Values
var foo = [];
$('#multiple :selected').each(function(i, selected){
foo[i] = $(selected).text();
});
$(document).ready(function () {
$('#sel').change(function(){ $('#sel option:selected').val(); });
give id to your select control
<SELECT id="sel">
with help of jQuery ( i suggest the logic, not the solution )
$(document).on('change', 'select', function(){
var selecetdOption = $(this).val();
alert(selecetdOption );
});​
Working Demo
You have wrong into your javascript because you have put
var select2=document.forms[0].sel2;
try to put
var select2=document.forms[1].sel2;
and it works!

specific e-mail pattern control with javascript

I've a textBox control and would like my users to only be able to enter ....#firm1.com' or '....#firm2.com'
How can I do this with a JavaScript function? The function must take a textbox value.
It doesn't make sense to give the user a textbox but allowing him to write two values only,
You should use a dropdown instead:
<select>
<option value="0"> #firm1.com </option>
<option value="1"> #firm2.com </option>
</select>
Update:
If you really have to use textbox for some unknown reason:
$('#textboxId').change(function(){
if (!this.value.match(/(#firm1.com|#firm2.com)$/g))
alert('invalid value');
});​
Live DEMO
Try this code-
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
var patt1 = "[A-Za-z0-9._%+-]+#firm(1|2).com";
function demoShowMatchClick(str) {
var re = new RegExp(patt1);
var m = re.exec(str);
if (m == null) {
document.getElementById("match").innerHTML = "no match";
} else {
document.getElementById("match").innerHTML = "match";
}
}
</script>
</head>
<body>
<span id="match"></span>
<input type="text" onkeyup="demoShowMatchClick(this.value)" />
</body>
</html>

Categories

Resources