Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
(jQuery) Setting a option to selected if value=a value
Old 08-09-2011, 02:48 PM (jQuery) Setting a option to selected if value=a value
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
hi there

Im trying to set a select option to selected if the value=a given value

I have the below but it just isnt happening.

$("#distance-type[value='"+data.distance_type+"']").attr('selected', 'selected');

data.distance_type can either = min or max,

My select is

<select name="distance-type" id="distance-type">
<option value="0">... Select ...</option>
<option value="min">Min</option>
<option value="max">Max</option>
</select>

so if data.distance_type=min i want the option min to be selected.

Any help would be appreciated.

thnx.

Steve
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
 
Register now for full access!
Old 08-10-2011, 02:34 AM Re: (jQuery) Setting a option to selected if value=a value
Novice Talker

Posts: 12
Name: Teodorescu Mihail
Location: Romania
Trades: 0
Hi,

How you get data.distance_type value ?
if you call data.distance_type inside JQuery function, be careful because data.distance_type means:
data -> div name or something like that
and
.distance_type -> that's a class selector !
So, together means a div name with class distance_type
If you don't use inside JQuery function, that name is allright.
So, you could write datadistance_type to avoid confusion, that's why JQuery see as a class selector if you write something with "dot" symbol inside JQuery function

look in my example : jsfiddle.net/HCSxg/

write "min" or "max" in input text and click outside input (everywhere in page) and you'll see an alert and select value will change

or

Code:
if(t=="min" || t=="max")
            $("#distancetype").attr("value",t);
where distancetype is distance-type
and t is datadistance_type, NO data.distance_type, I explained you above why not.

Last edited by Sonic3R; 08-10-2011 at 02:42 AM..
Sonic3R is offline
Reply With Quote
View Public Profile
 
Old 08-10-2011, 04:31 AM Re: (jQuery) Setting a option to selected if value=a value
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
The dot operator is also used to access object attributes, like a parsed JSON object or a normal array.

I think your issue lies in the use of the attr() function. If I remeber correctly, when used with <select> tags the second parameter should be boolean, as in

$("#distance-type[value='"+data.distance_type+"']").attr('selected', true);


EDIT: Or actually, can't you use the val() function?
$("#distance-type").val(data.distance_type);
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 08-10-2011 at 04:36 AM..
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 08-10-2011, 06:49 AM Re: (jQuery) Setting a option to selected if value=a value
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
it is access to a json parameter. ( data.distance_type )

im going to try using the val parameter used by Sonic3 and suggested by your self lizciz

Sonic3 Thanks very much for you input and the example you created for me.
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 08-10-2011, 04:12 PM Re: (jQuery) Setting a option to selected if value=a value
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
ok thanks for all your inputs,

the below worked which is kind of a selection of bot your comments together

$("#distance-type").attr("value", data.distance_type);

Thanks for your help

much appreciated.

Steve
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 08-11-2011, 03:50 AM Re: (jQuery) Setting a option to selected if value=a value
Novice Talker

Posts: 12
Name: Teodorescu Mihail
Location: Romania
Trades: 0
My pleasure numbenator
Sonic3R is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to (jQuery) Setting a option to selected if value=a value
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.63061 seconds with 12 queries