CHANGE FUNCTION IN JQUERY

Questions about programming languages and debugging
Post Reply
User avatar
intern3t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 119
Joined: 18 Aug 2010, 02:06
13

CHANGE FUNCTION IN JQUERY

Post by intern3t »

Code: Select all

<select name="sweets" multiple="multiple">
    <option>Chocolate</option>
    <option value= “test1”>Candy</option>

    <option value=”test2”>Taffy</option>
    <option selected="selected">Caramel</option>
</select>
<script>
    $("select").change(function () {
          var str = "";
          $("select option:selected").each(function () {
                str += $(this).text() + " ";
              });
          $("div").text(str);
        })
        .change();
</script>
This code will gets the text for each selected option and writes them in the div.
example: when you click on the caramel option, it will draw caramel under the option box.
Is there a way I can make it to draw something else to the div instead of the ones in the option. Sorry for bad english.

User avatar
intern3t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 119
Joined: 18 Aug 2010, 02:06
13

Re: CHANGE FUNCTION IN JQUERY

Post by intern3t »

Code: Select all

<select name="agegrade" size="1" onchange='checkvalue(this.value)' id="agegrade">
                <option>Please Select...</option>
                <option value="NAME1">1908  -  1911</option>
                <option value="NAME2">1912  -  1915</option>
                <option value="NAME3">1916  -  1919</option>
</select>
<input type="text" name="Nameofagegrade" id="Nameofagegrade" style='display:none' value="this is just testing" readonly="readonly" />

function checkvalue(val)
{
    if(val==="NAME1")
       document.getElementById('Nameofagegrade').style.display='block';
    else
       document.getElementById('Nameofagegrade').style.display='none'; 
}
with this, the block will be displayed



i want a different value to be printed in the same block when NAME2 and NAME3 is selected. how do i go about it? please help

cwdykarn
suck-o-fied!
suck-o-fied!
Posts: 77
Joined: 19 Dec 2012, 10:15
11

Re: CHANGE FUNCTION IN JQUERY

Post by cwdykarn »

try something like

Code: Select all

if () {
} elseif(){
} else{
}

source http://css-plus.com/2011/07/jquery-if-else-statements/

regards

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: CHANGE FUNCTION IN JQUERY

Post by ayu »

As per cwdykarns suggestion, a bunch of "if/else if"-statements would work

Code: Select all

<html>
	<head>
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
	</head>
	<body>
		<script>
			$(document).ready(function(){
			    $("#sweets").change(function () {
			
			          var str = "";
			          $("select option:selected").each(function () {
					str = $(this).text();
			
					if(str == "Chocolate")
						str = "Stuff";
					else if(str == "Candy")
						str = "Something else";
					else if(str == "Taffy")
						str = "Weeeee";
					else if(str == "Caramel")
						str = "I like pie";
					else
			                	str = "You have to select something";
			              });
			          $("#adiv").text(str);
			        })
			        .change();
			});
		</script>

		<select id="sweets" name="sweets" multiple="multiple">
		    <option>Chocolate</option>
		    <option value= “test1”>Candy</option>
		
		    <option value=”test2”>Taffy</option>
		    <option selected="selected">Caramel</option>
		</select>

		<div id="adiv"></div>
	</body>
</html>
"The best place to hide a tree, is in a forest"

User avatar
intern3t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 119
Joined: 18 Aug 2010, 02:06
13

Re: CHANGE FUNCTION IN JQUERY

Post by intern3t »

Thank you very much cats. the script is working but , when i fix it into the page where i want it to be, and i click on it, it doesn't display on the apdiv. Attached is the page along with the necessary scripts for the head. please download and review, i don't really know where the fault is from. here is the script :

Code: Select all

$(document).ready(function(){             
$("#agegrade").change(function () {                   
var str = "";                   
$("select option:selected").each(function () {               
str = $(this).text();               
if(str == "1900-1903")                  
str = "NAME TO BE PROVIDED";               
else if(str == "1904-1907")                  
str = "NAME TO BE PROVIDED";               
else if(str == "1908-1911")                  
str = "NAME TO BE PROVIDED";               
else if(str == "1912-1915")                  
str = "NAME TO BE PROVIDED";               
else if(str == "1916-1919")                  
str = "EGBE NA UGO";
else if(str == "1920-1923")                  
str = "NAME TO BE PROVIDED";               
else if(str == "1924-1927")                  
str = "IGWEBUIKE";               
else if(str == "1928-1931")                  
str = "ANYILIGWE";
else if(str == "1932-1935")                  
str = "NAME TO BE PROVIDED";               
else if(str == "1936-1939")                  
str = "NAME TO BE PROVIDED";               
else if(str == "1940-1943")                  
str = "UCHE BU UKO";
else if(str == "1944-1947")                  
str = "NAME TO BE PROVIDED";               
else if(str == "1948-1951")                  
str = "OFU OBI";               
else if(str == "1952-1955")                  
str = "ANULIKA";
else if(str == "1956-1959")                  
str = "IFUNANYA";               
else if(str == "1960-1963")                  
str = "NWANNEKA";               
else if(str == "1964-1967")                  
str = "NAME TO BE PROVIDED";
else if(str == "1968-1971")                  
str = "YET TO BE GROUPED";               
else if(str == "1972-1975")                  
str = "YET TO BE GROUPED";               
else if(str == "1976-1979")                  
str = "YET TO BE GROUPED";
else if(str == "1980-1983")                  
str = "YET TO BE GROUPED";               
else if(str == "1984-1987")                  
str = "YET TO BE GROUPED";               
else if(str == "1988-1991")                  
str = "YET TO BE GROUPED";
else if(str == "1992-1995")                  
str = "YET TO BE GROUPED";               
else if(str == "1996-1999")                  
str = "YET TO BE GROUPED";
else if(str == "2000-2003")                  
str = "YET TO BE GROUPED";                              
else
str = "Please Select An Item";
                       });                   
$("#adiv").text(str);
 })                 
 .change();         
 });
 </script>      
<select id="agegrade" name="agegrade" multiple="multiple">          
<option  selected="selected">1900-1903</option>
                <option>1904-1907</option>
                <option>1908-1911</option>
                <option>1912-1915</option>
                <option>1916-1919</option>
                <option>1920-1923</option>
                <option>1924-1927</option>
                <option>1928-1931</option>
                <option>1932-1935</option>
                <option>1936-1939</option>
                <option>1940-1943</option>
                <option>1944-1947</option>
                <option>1948-1951</option>
                <option>1952-1955</option>
                <option>1956-1959</option>
                <option>1960-1963</option>
                <option>1964-1967</option>
                <option>1968-1971</option>
                <option>1972-1975</option>
                <option>1976-1979</option>
                <option>1980-1983</option>
                <option>1984-1987</option>
                <option>1988-1991</option>
                <option>1992-1995</option>
                <option>1996-1999</option>
                <option>2000-2003</option>
              </select>
<div id="adiv"></div> 
This works. but when i put it on the page where i want it to be, it doesn't work.please download and review.thanks
Attachments
register.rar
(105.11 KiB) Downloaded 77 times

User avatar
intern3t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 119
Joined: 18 Aug 2010, 02:06
13

Re: CHANGE FUNCTION IN JQUERY

Post by intern3t »

i have finally fixed this, i used this

Code: Select all

$(function(){             
  $("#agegrade").change(function () {                           
    $("#adiv").text($("select option:selected:last").val());
  }).change();         
});
instead of the if and else.thanks everyone

Post Reply