Let's break that statement down into individual statements: var selector = "[id$=" + b1 + "] input"; var value = ( "Hide" ? "Show" : "Hide" ); $(selector).val( value );
Now what is in the variable 'value'? You can try alerting it: var value = ( "Hide" ? "Show" : "Hide" ); alert( value ); // always alerts 'Show' You can see what the problem is now: the expression ( "Hide" ? "Show" : "Hide" ) always evaluates to "Show". What you probably meant to do was something like this: var $input = $("[id$="+b1+"] input"); $input.val( $input.val() == "Hide" ? "Show" : "Hide" ); Or written out more step-by-step: var $input = $("[id$="+b1+"] input"); var value = ( $input.val() == "Hide" ? "Show" : "Hide" ); $input.val( value ); -Mike On Fri, Oct 9, 2009 at 12:35 PM, hsfrey <hsf...@gmail.com> wrote: > > Giovanni: > > Thanks for the suggestion, but, unfortunately it doesn't do > anything . :-( > > However, I tried the following: > > $("[id$="+b1+"] input").val("Hide"?"Show":"Hide"); > > This actually does switch the label to "show", but not back to > "hide" when clicked again. > BUT - I'm as confused about why it DOES work the first time as about > why it doesn't subsequently. <g> > > The attribute "value" isn't actually the First element of the > "input" tag; "type" is. Why does .val change the Second element? > That's why I was using .attr before instead of .val) > (Remember, the target is <input type=button value="Hide" onclick="togl > ('box1')"/>) > > Also - The Docs say that .val with one argument GETS the value of > the first matched element. > Why does this call with one argument SET the first element? > Or, DOES it set the element? Even though it LOOKS like it's set, it > ACTS on subsequent calls as though it hasn't been. > > Harvey > > > On Oct 8, 11:19 pm, Giovanni Battista Lenoci <gian...@gmail.com> > wrote: > > hsfrey ha scritto: > > > > > function togl(b1) { > > > var t1 = document.getElementById(b1).getElementsByTagName("input"); > > > var labl = t1[0].value; > > > t1[0].value = labl=="Hide"?"Show":"Hide"; > > > ... > > > } > > > > > In addition, rather than just Match the ID, I want to match the ID > > > which ENDS in the string contained in b1. > > > A simple "#"+b1 won't do that, which is why I used ["[id$="+b1="]". > > > > > The target of the JS is the html: > > > <div id="box1" ...> > > > <input type=button value="Hide" onclick="togl('box1')"/> > > > ... > > > </div> > > > > If I understood, in your markup the button will have an id like > > "something_box1", right? > > > > If you can change your markup, i suggest to you to group elements using > > a class. by the way your first code: > > > > $(("[id$="+b1+"]") input).attr(value, attr(value)=="Hide"?"Show":"Hide"); > > > > should be > > > > $("[id$="+b1+"] input").val($(this).val()=="Hide"?"Show":"Hide"); > > > > Bye > > > > (i've not tested the code, but I think is right :P) > > > > -- > > gianiaz.net - web solutions > > via piedo, 58 - 23020 tresivio (so) - italy > > +39 347 7196482 >