Javascript: Out of scope? - javascript

Re-Edited:
I went over to W3schools and played around in there javascript editor window to see what was going on. Here is a shorter script.
<html>
<body>
<script type="text/javascript">
var eType = document.getElementById("Type")
</script>
<select class="field select addr" id="Type">
<option value="Type:L" selected="selected">Location</option>
<option value="Type:C">Corporate</option>
<option value="Type:R">Remittama</option>
<option value="Type:M">Mailing</option>
</select>
<script type="text/javascript">
document.write(eType);
</script>
</body>
</html>
I did what ThiefMaster suggested on how to get the element by not using the .text or .value but as you can see if you run this code I still get a null at this step of the process. From what I can see and many suggestions I am doing this correctly. I know I must be missing something. Can anyone see it?

You're trying to get the element before you've actually put it on the page. Fix the order, and don't use document.write.
<html>
<body>
<select class="field select addr" id="Type">
<option value="Type:L" selected="selected">Location</option>
<option value="Type:C">Corporate</option>
<option value="Type:R">Remittama</option>
<option value="Type:M">Mailing</option>
</select>
<script type="text/javascript">
var eType = document.getElementById("Type")
document.body.appendChild(document.createTextNode(eType));
</script>
</body>
</html>

Your second code block is almost correct; you need to use the select element itself instead of its (non-existent) value:
var elem = document.getElementById("Type");
var type = elem.options[elem.selectedIndex].value;
Demo: http://jsfiddle.net/ThiefMaster/nw3nN/
If you had jQuery available you could drastically reduce the amount of code necessary:
var type = $('#Type').val();

Related

Changing the font of a document using a select dropdown in javascript

I am trying to figure out how to make this work. Please let me know if there is something I can change to make this code work without changing it completely.
<select name="fontPick">
<option value="0">Font</option>
<option value="1">Calibri</option>
<option value="2">Candara</option>
<option value="3">Tahoma</option>
</select>
<script type="text/javascript">
document.querySelector('[name="fontPick"]').addEventListener('change', function () {
var fonts = this.options[this.selectedIndex].textContent;
document.body.style.fontFamily = fonts;
});
</script>
<script type="text/javascript> you forgot the closing " make it
<script type="text/javascript">

Preview Image with Values from Select with Javascript

in my script, I have a select-field:
<select name="src[jumpMenu]" id="jumpMenu" onchange="ChangeImage(this.value,'preview');">
<option value="A">AStyle</option>
<option value="B">BStyle</option>
<option value="C">CStyle</option>
</select>
and I got this Javascript below:
<script language="Javascript" type="text/javascript">
<!--
function ChangeImage(newimage,imageid) {
if(document.getElementById(imageid).src != newimage)
document.getElementById(imageid).src = newimage;
}
// -->
</script>
and here is my problem:
I can't change the values because of the dependence for the other files (which I won't change).
Do you know any solution how to switch "A" to "img/src/A_top.png" for example and get it working with this script?
Thanks.
document.getElementById(imageid).src = "img/src/"+newimage+"_top.png";

html form select tag

This is kinda annoying, but even though I have worked with html/css and tiny bit of jquery I haven't yet learned much about forms etc.
I have a problem and I cant google the answer, but im sure some of you hardcore devs can tell me this quick!?
I need to make a form that could look like this:
HTML;
<form class="slct-form">
<select>
<option value="choose">Choose</option>
<option value="100.000">100.000</option>
<option value="150.000">150.000</option>
<option value="200.000">200.000</option>
</select>
</form>
I need that when I choose one of the elements in my dropdown form it should go to another url... Is this not possible? AND preferable without jquery!
If you add an id to your select, you can access it via plain javascript like this:
var selectmenu = document.getElementById("selectMenu");
selectmenu.onchange = function() {
var selectedOption = this.options[this.selectedIndex];
if (selectedOption.value != "choose")
window.location.assign = "/page.php?selectvalue=" + selectedOption.value;
}
You could do something like this:
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<script type="text/javascript">
function donav(dd)
{
var navUrl = "";
switch(dd.selectedIndex)
{
case 1:
navUrl = "http://www.google.com/";
break;
case 2:
navUrl = "http://www.bing.com";
break;
}
document.location.href = navUrl;
}
</script>
</head>
<body>
<form class="slct-form">
<select onchange="donav(this)">
<option value="choose">Choose</option>
<option value="100.000">100.000</option>
<option value="150.000">150.000</option>
<option value="200.000">200.000</option>
</select>
</form>
</body>
</html>
If i understand you corretly you want to bring a value from one page to another?
You could use the $_GET['name'] with php for that.

How do I add JavaScript code on a webpage?

Could someone please tell me how to get this code working on a webpage? Particularly what should go in the header?
http://jsfiddle.net/mekwall/TJcP4/1/
Sorry if this is a basic question...steep learning curve!
Thanks
Your code is using the jQuery JavaScript library ... so your head will need to contain :
<script type="text/javascript" src="<jquery url>"></script>
Replace the <jquery url> with a valid url to the jQuery library. I suggest you use the Google CDN for the url or alternatively download a copy and store it on your server -> http://docs.jquery.com/Downloading_jQuery#Download_jQuery
Then to ensure your code runs once the DOM is ready wrap all of your JavaScript within the following :
$(document).ready(function() {
// your code here
});
Docs for ready() here
If your going to be using jQuery more I suggest you start reading here http://docs.jquery.com/How_jQuery_Works and if you going to learn JavaScript, you can't go wrong with reading this too -> https://developer.mozilla.org/en/JavaScript/Guide
Copy the code below, save it with a .html extension (e.g. test.html) and then double click to open.
<html>
<head>
<title>Page Title here</title>
</head>
<body>
<select id="t_dermal_name">
<option value="t_default_dermal">-- Choose --</option>
<option value="1" rel="30">Between Eyebrows</option>
<option value="7" rel="30">Individual Line Softening</option>
<option value="2" rel="30">Lip Contouring</option>
</select>
<select id="t_wrinkle_name">
<option value="t_default_wrinkle">-- Choose --</option>
<option value="1" rel="30">Between Eyebrows</option>
<option value="7" rel="30">Individual Line Softening</option>
<option value="2" rel="30">Lip Contouring</option>
</select>
<span id="output"></span>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script><!--You can use a local version of jquery-->
<script type="text/javascript">
$(document).ready(function(){
function onSelectChange(){
var total = 0,
dermal = $("#t_dermal_name").find('option:selected'),
wrinkle = $("#t_wrinkle_name").find('option:selected');
if(value = dermal.attr('rel')){
total += parseInt(value);
}
if(value = wrinkle.attr('rel')){
total += parseInt(value);
}
$("#output").html(total);
}
$("#t_dermal_name").change(onSelectChange);
$("#t_wrinkle_name").change(onSelectChange);
});
</script>
</body>

How to pass the selected value to java script function inside of select tag in html?

<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>
Hi i need to pass the selected value immediately on inside of this select tag so pls some one help me
Not that I agree with how you're doing things here (in side the tag), technically it is possible to do what you ask by the following:
<form>
<SELECT NAME="sel" onChange="split(this.value)">
<OPTION VALUE=1>Milk</option>
<OPTION VALUE=2>tea</option>
<OPTION VALUE=3>water</option>
<OPTION VALUE=4>coffee</option>
</SELECT>
</form>
You cannot pass the value directly to the handler but you can get the values in it, I'd recommend to do this in code and not use inline event handlers:
var select = document.forms[0].sel;
select.onchange = function(){
var value = select.options[select.selectedIndex].value; // to get Value
var text = select.options[select.selectedIndex].text; // to get Text
}
Here's a working example:
http://jsfiddle.net/c2SrV/
<html>
<head>
<script>
function split(value)
{
alert(value);
}
</script>
</head>
<body>
<form>
<SELECT NAME="sel" onChange="split(value)">
<OPTION VALUE=1>Milk</option>
<OPTION VALUE=2>tea</option>
<OPTION VALUE=3>water</option>
<OPTION VALUE=4>coffee</option>
</SELECT>
</form>
<body>
</html>
use "value". hope this was helpful
this.options[this.selectedIndex].value
*assuming you want to put it inline inside the onchange attribute

Categories