11 Replies - 770 Views - Last Post: 28 January 2013 - 04:58 PM

#1 BasementArtScott  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 49
  • Joined: 11-July 09

Changing Select Box with Input from a Text Box

Posted 28 January 2013 - 03:43 PM

I have written this JS code that is supposed to alter the contents of a select box (pull down) menu upon having a value entered in a text box. In this case if a lesser value is entered in the text box than is in the pull down, the pull down menu is reduced to a lesser value.

I'm testing the code using firebug, but it keeps getting to the var element point and jumping into a JQuery method that isn't anywhere near this block of code or related. I assume there is somethign wrong with it, but if I could get an extra set of eyes on this it would be a great help.

Thanks.

//Functions


function compareAmts($FIELDSwitchText, $FIELDSwitchPull)
{
	var SwitchAdv = 0;
	temp = $("#FIELD_SwitchPull").val();
    var SwitchComm = parseInt(temp.replace(/\,/g,''));
	
	if($("#FIELD_SwitchText").val()!=""){
		SwitchAdv = parseInt($("#FIELD_SwitchText").val());
	}
	
	
    var element = $FIELDSwitchText.get(0),
    options = element.options,
    i = element.selectedIndex,
    atMost = +$FIELDSwitchPull.val(); // cast to number

    while (i >= 0 && options[i].value >= atMost) {
        --i;
    }

    element.selectedIndex = i;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Changing Select Box with Input from a Text Box

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,020
  • Joined: 08-June 10

Re: Changing Select Box with Input from a Text Box

Posted 28 January 2013 - 03:59 PM

View PostBasementArtScott, on 28 January 2013 - 11:43 PM, said:

I'm testing the code using firebug, but it keeps getting to the var element point and jumping into a JQuery method that isn't anywhere near this block of code or related. I assume there is somethign wrong with it, but if I could get an extra set of eyes on this it would be a great help.

does that mean there is an error message?

regarding your code, what is the while() loop supposed to do? and what do you mean by "[] the pull down menu is reduced to a lesser value"?
Was This Post Helpful? 0
  • +
  • -

#3 BasementArtScott  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 49
  • Joined: 11-July 09

Re: Changing Select Box with Input from a Text Box

Posted 28 January 2013 - 04:02 PM

I'm new to Firebug, and just now found the error message. I couldn't find one until this second..

It is

var element = $FIELDSwitchText.get(0) on this line and state Type Error this line is not a Function. The While loop compares the selected element from the pull down menu to the variable in the text box, and while the value is less than the other it switches it out. I'm pretty sure the while section is fairly spot on, but that line I can see is now problematic.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,020
  • Joined: 08-June 10

Re: Changing Select Box with Input from a Text Box

Posted 28 January 2013 - 04:08 PM

View PostBasementArtScott, on 29 January 2013 - 12:02 AM, said:

var element = $FIELDSwitchText.get(0) on this line and state Type Error this line is not a Function.

if $FIELDSwitchText that you pass to compareAmts() is not a valid jQuery object, this is the error that occurs. though that cant be validated with the code given, wed need to see the code that calls that function.

View PostBasementArtScott, on 29 January 2013 - 12:02 AM, said:

The While loop compares the selected element from the pull down menu to the variable in the text box, and while the value is less than the other it switches it out.

nope. the loop itself does nothing beyond decreasing i. and even the line afterwards only sets the selection. the <select> itself remains unchanged.
Was This Post Helpful? 0
  • +
  • -

#5 BasementArtScott  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 49
  • Joined: 11-July 09

Re: Changing Select Box with Input from a Text Box

Posted 28 January 2013 - 04:14 PM

What I need is for I to decrement and then the select to change. How would I go about that? Anyway, I discovered the error is within firebug itself. Apparently anything that has a $ directly preceeding it qualifies as a function, and that is not a function, but a local variable.

This post has been edited by Dormilich: 28 January 2013 - 04:15 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,020
  • Joined: 08-June 10

Re: Changing Select Box with Input from a Text Box

Posted 28 January 2013 - 04:20 PM

View PostBasementArtScott, on 29 January 2013 - 12:14 AM, said:

What I need is for I to decrement and then the select to change.

what does that mean? does that mean you want to set the <select>s selected value to the one in the textbox?

View PostBasementArtScott, on 29 January 2013 - 12:14 AM, said:

Anyway, I discovered the error is within firebug itself. Apparently anything that has a $ directly preceeding it qualifies as a function, and that is not a function, but a local variable.

then you have a very different FireBug than I have. Even I use $ as a prefix for jQuery objects and I have never seen such behaviour.
Was This Post Helpful? 0
  • +
  • -

#7 BasementArtScott  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 49
  • Joined: 11-July 09

Re: Changing Select Box with Input from a Text Box

Posted 28 January 2013 - 04:25 PM

I just read somewhere that $blank in firebug gets treated as a function. The error states that the .get is being treated as a function.

OK, so what I am trying to do is this.

Let's say customer puts in a number 2000 in the text box, but 3000 in the pulldown menu, I would like the pull down menu (select box) to reduce from 3000 to 2000. There are x number of selections pre-configure in the select box, and I would like them to decrement to the lowest comparable number.

Would something like this at the end work ?

	return i.replace (netadv, compareAmts);


This post has been edited by BasementArtScott: 28 January 2013 - 04:31 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,020
  • Joined: 08-June 10

Re: Changing Select Box with Input from a Text Box

Posted 28 January 2013 - 04:30 PM

View PostBasementArtScott, on 29 January 2013 - 12:25 AM, said:

I just read somewhere that $blank in firebug gets treated as a function.

never heard of that before.


View PostBasementArtScott, on 29 January 2013 - 12:25 AM, said:

The error states that the .get is being treated as a function.

makes sense. you call it as such.


View PostBasementArtScott, on 29 January 2013 - 12:25 AM, said:

Let's say customer puts in a number 2000 in the text box, but 3000 in the pulldown menu, I would like the pull down menu (select box) to reduce from 3000 to 2000. There are x number of selections pre-configure in the select box, and I would like them to decrement to the nearest one.

Id try as simple as setting the <select>s value (whether in DOM or in jQuery).

btw., why can the customer select a single value in two different form elements?
Was This Post Helpful? 0
  • +
  • -

#9 BasementArtScott  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 49
  • Joined: 11-July 09

Re: Changing Select Box with Input from a Text Box

Posted 28 January 2013 - 04:38 PM

How would I set the selects value elsewhere?

Anywhere the reason is basically a want vs actuality situation.

Customer wants x value, but provides y number. Well Y number is less than x value so x value needs to automatically reduce accordingly.
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,020
  • Joined: 08-June 10

Re: Changing Select Box with Input from a Text Box

Posted 28 January 2013 - 04:44 PM

View PostBasementArtScott, on 29 January 2013 - 12:38 AM, said:

How would I set the selects value elsewhere?

you set the value property?


View PostBasementArtScott, on 29 January 2013 - 12:38 AM, said:

Customer wants x value, but provides y number. Well Y number is less than x value so x value needs to automatically reduce accordingly.

drop the x value. if the customer wants y (and thats allowed), why bothering with x?
Was This Post Helpful? 0
  • +
  • -

#11 BasementArtScott  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 49
  • Joined: 11-July 09

Re: Changing Select Box with Input from a Text Box

Posted 28 January 2013 - 04:54 PM

Looking at your explanation versus what I actually have to do I can tell that isn't going to work. The system I am working with is set up for specifically for a reason, so I have to keep the x and and the y values.

Would a variation on that return statement I asked about earlier resolve this issue? And as for the earlier error, I seemingly would just have to replace those $values with the ones accurate to the functions in jquery, correct?
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,020
  • Joined: 08-June 10

Re: Changing Select Box with Input from a Text Box

Posted 28 January 2013 - 04:58 PM

View PostBasementArtScott, on 29 January 2013 - 12:54 AM, said:

The system I am working with is set up for specifically for a reason, so I have to keep the x and and the y values.

since I dont know anything about that system, I can only recommend based on the given information, which is, admittedly, not very much.


View PostBasementArtScott, on 29 January 2013 - 12:54 AM, said:

Would a variation on that return statement I asked about earlier resolve this issue?

I see no return statement anywhere.


View PostBasementArtScott, on 29 January 2013 - 12:54 AM, said:

And as for the earlier error, I seemingly would just have to replace those $values with the ones accurate to the functions in jquery, correct?

yes, the $values need to be jQuery objects. but right now I have no idea what you actually pass.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1