Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

jquery ui slider problem, ui.value passes number rather than text value.

Posted on 2009-07-05
12
Medium Priority
?
599 Views
Last Modified: 2013-11-11
hi there, im using jquery ui to create my sliders from a select input. My code works in that it calls a function everytime the slider value changes but ui.value only passes the numerical value of the option rather than the the actual value of the select box, how do i fix this?

my code:

$('#budget').selectToUISlider({
labels: 0,
sliderOptions: {
change:function(e, ui) {
paging('1', '','','1', ui.value , '','' , '');
}
}
});


<select name="budget" id="budget">
  <option value="option1" selected="selected">All</option>
  <option value="option2">asd</option>
  <option value="option3" >dfsdf</option>
  <option value="">sdfsdf</option>
</select>
EXAMPLE: looking at the select above, i want the code to pass the value "option1" to my function, however ui.value only passes "1" while otion 2 passes "2" and so on.

Slider resources:
http://www.filamentgroup.com/lab/update_jquery_ui_slider_from_a_select_element_now_with_aria_support/

http://jqueryui.com/demos/slider/
0
Comment
Question by:cookiejest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24779392
I don't know jQuery, but I'm assuming the value is the index of the option, so, try ...

$('#budget').option(ui.value).value
0
 

Author Comment

by:cookiejest
ID: 24779539
thats what i thought but paging('1', '','','1', $('#budget').option(ui.value).value , '','' , ''); does not work?!
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24779572
Can you try ...


change:function(e, ui) {
alert(ui);
alert(ui.value);
paging('1', '','','1', ui.value , '','' , '');
}


Oh. Is it ...

paging('1', '','','1', $('#budget').options(ui.value).value , '','' , '');


option_S_

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cookiejest
ID: 24781221
tried that but it didnt work, ive been looking at the slider ui documentation, i think it might be one of the methods but im not sure how to implement them.

    .slider( 'option' , optionName , [value] )

or maybe

    .slider( 'value' , [value] )

http://jqueryui.com/demos/slider/#method-option
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24784167
Can you show me a URL?
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24784231
Can you change ...

paging('1', '','','1', ui.value , '','' , '');

to

alert(ui.value); // Should be the value.

Does the value change appropriately?

If so, change this to ...

alert(parseInt(ui.value)); // Should still be the value but as a number rather than a string.
alert($('#budget').name); // Should be budget.
alert($('#budget').options); // Should be an array.
alert($('#budget').options(parseInt(ui.value,10)); // Should be an option object.
alert($('#budget').options(parseInt(ui.value,10).value); // Should be the value for the option.

.slider('option', ...) is about getting the SLIDER's options, not the SELECT's <option>'s.

0
 

Author Comment

by:cookiejest
ID: 24785395
hi there thank you for responding,

alert(ui.value); works

alert(parseInt(ui.value)); works

all the rest resturn "undefined".
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24785441
Right. Can you show me the complete HTML source? Ideally an online link.

I use prototype, not jquery, so my syntax may be off.

With $('#budget') I want to get the select which id is 'budget'.

Then I want to find the options property (which is the list of options on the select).

Then I want to find the specific option as specified by ui.value.

Then I want to find the value attribute for that option.

0
 

Author Comment

by:cookiejest
ID: 24785513
Here is the full html, im sorry but i cannot provide an online link at the moment!
<script type="text/javascript"> 
 $(function(){
 
 
$('#posted').selectToUISlider({
labels: 0,
sliderOptions: {
change:function(e, ui) {
//alert(parseInt(ui.value)); // Should still be the value but as a number rather than a string.
alert($('#posted').name); // Should be budget.
//alert($('#posted').options); // Should be an array.
//alert($('#posted').options(parseInt(ui.value,10)); // Should be an option object.
//alert($('#posted').options(parseInt(ui.value,10).value); // Should be the value for the option.
 
//paging('1', '','','1', '', ui.value , '', '');
}
}
}); 
}); 
 
</script> 
 
<select name="posted" id="posted"> 
  <option value="" selected="selected">All</option> 
  <option value="1246804007"> Less than 24 Hours ago</option> 
  <option value="1246717607" >Less than 2 days ago</option> 
  <option value="1246631207">Less than 3 days ago</option> 
  <option value="1246544807">Less than 4 days ago</option> 
  <option value="1246458407">Less than 5 days ago</option> 
  <option value="1246372007">Less than 6 days ago</option> 
  <option value="1246285607">Less than 7 days ago</option> 
  <option value="1245680807">Less than 2 weeks ago</option> 
</select> 

Open in new window

0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 2000 total points
ID: 24785820
Hi.

Got it working.

Change the locations of the JS files.

I didn't get a slider (not sure what you need for that).

It didn't look nice in FF or Chrome, but the selecting worked in FF (I just wanted the debugs).

Run the script below in Firefox with Firebug.


Essentially, the important bit is ...

$('#posted')[0].options[parseInt(ui.value,10)].text

or

$('#posted')[0].options[parseInt(ui.value,10)].value

depending upon what you want to use.
<html>
<head>
<script type="text/javascript" src="file:///Z:/slider/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="file:///Z:/slider/js/jquery-ui-1.7.1.custom.min.js"></script>
<script type="text/javascript" src="file:///Z:/slider/js/selectToUISlider.jQuery.js"></script>
</head>
<body>
<select name="posted" id="posted"> 
  <option value="" selected="selected">All</option> 
  <option value="1246804007"> Less than 24 Hours ago</option> 
  <option value="1246717607" >Less than 2 days ago</option> 
  <option value="1246631207">Less than 3 days ago</option> 
  <option value="1246544807">Less than 4 days ago</option> 
  <option value="1246458407">Less than 5 days ago</option> 
  <option value="1246372007">Less than 6 days ago</option> 
  <option value="1246285607">Less than 7 days ago</option> 
  <option value="1245680807">Less than 2 weeks ago</option> 
</select>
<script type="text/javascript">
$
    (
    function()
        {
        $('#posted')
            .selectToUISlider
                (
                    {
                    labels: 0,
                    sliderOptions:
                        {
                        change:function(e, ui)
                            {
                            console.info('parseInt(ui.value, 10)', parseInt(ui.value));
                            console.info('#posted[0]', $('#posted')[0]);
                            console.info('#posted[0].name', $('#posted')[0].name);
                            console.info('#posted[0].options', $('#posted')[0].options);
                            console.info('#posted[0].options[parseInt(ui.value, 10)]', $('#posted')[0].options[parseInt(ui.value,10)]);
                            console.info('#posted[0].options[parseInt(ui.value, 10)].value', $('#posted')[0].options[parseInt(ui.value,10)].value);
                            console.info('#posted[0].options[parseInt(ui.value, 10)].text', $('#posted')[0].options[parseInt(ui.value,10)].text);
                            }
                        }
                    }
                );
        }
    ); 
</script> 
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:cookiejest
ID: 31599896
thats exactly what i needed and I think it will help many others as well!
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24786041
Sorry it took so long, but glad to have helped.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question