Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 27-12-2010, 11:17 PM   #1 (permalink)
In The Zone
 
cute.bandar's Avatar
 
Join Date: Jan 2010
Posts: 408
Default Javascript passing option's value to a function


HI, I am making a site for which require a bit of javascript magic. asking for your help because I am still learning javascript.

Javascript Code -
Code:
 function changeMap()
    {
       imagesource =  "http://maps.google.com/maps/api/staticmap?size=500x500&maptype=hybrid&zoom=16&sensor=false&markers=color:blue|label:K|28.541250,77.204100" ;
       mapimage.src  = imagesource ;
    }
Html code -
Code:
 <select name="choose_colony" id="choose_colony" size="8" onchange="changeMap()" style="float: left;">
        <option value="1" >Big apartments</option> 
        .
        .
        <option value="999">plaza</option>
        </select>
        <img name="mapimage" src="" alt="Select your Colony" style="float: right;">
In this whenever a selection on the listbox is made changeMap() is called and an image is loaded. What I want to do is, pass the 'value' of the <option> selected, to the function changeMap , so that it can be used there.
How ?
thanks

Edit: nvmind, I found a solution:
changemap has to be changed to -
Quote:
changeMap(this.options[this.selectedIndex].value)
I still don't understand why this works though..
__________________
Computer parts search Engine(for india) : http://goo.gl/Sg8Yv

Last edited by cute.bandar; 28-12-2010 at 12:22 AM.
cute.bandar is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 28-12-2010, 12:33 PM   #2 (permalink)
Excessive happiness
 
furious_gamer's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 2,974
Default Re: Javascript passing option's value to a function

Because this is the proper way of doing. For all elements in HTML, this.value will get the value of the current element and for a option box u have to use what u posted above, or simple this.value will do.

Before doing anything big, please learn some good old basic Javascript, so that its easy for you in further development.
__________________
My First Android phone : Samsung Galaxy SL i9003 - Rooted & Gingerbread XXKPQ
Updated : superteekz_V2 ROM for XXKPQ.

PS Request
furious_gamer is offline   Reply With Quote
Old 28-12-2010, 02:10 PM   #3 (permalink)
Torrent Lover :)
 
akshaykapoor_3's Avatar
 
Join Date: Nov 2006
Location: Chandigarh
Posts: 182
Default Re: Javascript passing option's value to a function

Also go through DOM Model in Javascript which is used to acces various element values in the form or on the page you have designed in HTML.

Simply put, DOM Model helps you access the variuos form elements in a logical heirarchical manner. <this.value> although acts as a shortcut and will work in all cases but the proper way to access would be in a somewhat logical statement such as:

document.<form name>.<element name>.<value> where form name,element name and value would be custom names assigned by you to the elements in the "id" attribute of tags. Hope that helps
akshaykapoor_3 is offline   Reply With Quote
Old 04-02-2011, 01:53 PM   #4 (permalink)
Apprentice
 
Nemes!s's Avatar
 
Join Date: Jul 2010
Location: Bangalore
Posts: 88
Default Re: Javascript passing option's value to a function

"this" is a keyword for referencing to the current instance of the object..in your case it is listbox..

Check out this article about this Javascript - The this keyword
__________________
Athlon II X4 635 @ 2.9Ghz || Biostar TA785G3 HD || G.skill 2X2Gb 1333Mhz DDR3 || WDC 500Gb Green || Corsair VX550W || CM Elite 330 || BenQ 2220HD || HTC Inspire 4G :))
Nemes!s is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:24 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2