views:

46

answers:

2

I am trying to remove the "_" in the "change_option". I managed to add the class ok but that's it

<select onchange="change_option('SELECT___100E___7',this.options[this.selectedIndex].value)" name="SELECT___100E___7" class="link_fix3">

Here is the code I have so far...

$("form select[onchange^='change_option']")
    .addClass('link_fix3')
    .attr('onchange', function(k,x){
     return x.replace('_','');})
A: 

Without knowing the internals, it seems you're trying to call changeoption(name, value), you can do this entirely in jQuery, like this:

$("form select[onchange^='change_option']")
  .addClass('link_fix3')
  .change(function() {
    changeoption(this.name, $(this).val());
  });

Though with more details, this can probably be even simpler, for example is giving the <select> a class initially an option? That would make the selector much cleaner, then you can leave the initial onchange attribute off completely...you have to admit this looks prettier :)

<select name="SELECT___100E___7" class="link_fix3">

Script like this:

$("form select.link_fix3").change(function() {
  changeoption(this.name, $(this).val());
});

If you post what your changeoption function looks like, I could improve the answer to show additional simplifications, if any of these options I'm offering are do-able...I know the programmers control over markup varies widely, so let us know what options you do have.

Nick Craver
Well the code posted does work!!!! The only reason for me adding the class is I though it work be needed to target the function but I see that will not be necessary so I removed the .addClass('link_fix3') code and it works just the same.Thanks!!!
@user - Welcome :) Be sure to accept answers via the checkmark on the left if it solves your problem, it also helps you get answers to future questions faster :)
Nick Craver
Well like I said it does work but I think it not excatly as I thought it should. It seems that it changes the function name after the onchange in activated. Sorry if I do not explain it in proper terms. Any way to change the name and not the name as a rsult of the action?I would expect the markup to be changed as soon as the page loads which is not the case here. I know it is working however because it is calling the changeoption function and not the change_option function. Hope you understand what I am trying to say?
@user - Is there a reason the original function needs to be there at all? It depends how you're viewing markup, firebug, chrome console? (IE is *highly* unreliable here, it shows the original source)...also this is an event, it's not like a normal attribute, so don't think of it the same way, attaching the correct event handler by binding like I have above is the better approach in most cases.
Nick Craver
No there is no reason for the original function to be there. Firebug BTW. I have a similar replace that Reigel helped me out before and that will show up in the markup but yours does not.See this message...http://stackoverflow.com/questions/2962860/prepend-href-with-same-class
Sorry if I misunderstood but let me clarify, the "onchange" is necessary, what is no longer necessary is the function name "change_option"
@user - If you just have an initial class, or another way to get the `<select>` with a jQuery selector, like `$("select.pickMe")` gets `<select class="pickMe">`, you can use my second solution above. This allows you to move all the script into an external file as well, keeping it even cleaner...what about this approach *doesn't* work for your site? It's the common way of doing things now since it offers numerous advantages, including smaller download/payload, faster loads, unobtrusive, easier to debug, etc.
Nick Craver
well I tried the your second answer and that works as well..$("form select").addClass("link_fix3");$("form select.link_fix3").change(function() {changeoption(this.name, $(this).val());});Just do not see the change_option changing in the markup like I would expect to see when the page finishes loading. Mystery to me but I can't complain, LOLIf you got nothing more you can think of I mark it answered, thanks so much
@user - You can see it bound in the console, if you do `$("form select").data("events")` you can see all bound handlers, for example `$("form select").data("events").change` will show the handler you just attached :)
Nick Craver
Well I wish I knew how to do that, but it is ok it works and that's what is important, just wished I knew why, lol but thats why your the expert and I am not, lol, thx again
A: 

just a thought... if you do this,

$("form select[onchange^='change_option']")
    .addClass('link_fix3')
    .attr('onchange', function(k,x){
     return x.replace('_','');})

the output would be,

onchange="changeoption('SELECT00E7',this.options[this.selectedIndex].value)" // notice your, 'SELECT___100E___7'

I suggest this,

$("form select[onchange^='change_option']")
        .addClass('link_fix3')
        .attr('onchange', function(k,x){
         return x.replace('change_option','changeoption');})

that is, if you really want to solve it that way...

Reigel
I tried this and it does not do work, but thx!!!