views:

301

answers:

1

Hi!

(I hope my title is comprehensible, it was hard to sum up.)

I have some script which change some text inside a form depending on the selected radio button as you can see here: http://jsbin.com/onowu/

The problem is that if some user enter some text and then click on a other radio button it will remove his text.

What I'm looking for is a way change the grey text ONLY when there isn't any text filled by the user. And if the user remove his text, it will fill blank with the text depending on the radio button selected.

You can see my code here: http://jsbin.com/onowu/edit

Thanks ;)

+5  A: 

Check to see if the input has the class "hint". Only change the text is it does.

Full example:

$(document).ready(function() {
    function setText(){
        var kf = this.title.split('|');
        if (kf.length < 0) return;
        if($('#' + this.name + '_text').hasClass('hint')){
            $('#' + this.name + '_text').val(kf[0]).addClass('hint');
        }
        $('#' + this.name + '_text').attr('title', kf[0]);
    }

    $("input[type='text']").inputdynvalue();
    $("input[type='radio']").click(setText);
});

This works by checking the input textbox for the class "hint" and changing the value only when it is found. As mentioned in the comments by Jonathan Sampson "the .focus() event removes the class "hint," and the .blur() event adds it again if no text has been provided".

I would also run your code through the html validation service provided by W3C. JavaScript can be funny if you do not have valid code. (Plus everyone should write valid html)

A also noticed you were including jQuery twice in your code, I would suggest you remove one of the one of them.


Updated (July 10th, 2009): Fixed a bug mentioned in the comments below.

MitMaro
++ | Further, the .focus() event removes the class "hint," and the .blur() event adds it again if no text has been provided. Just incase MitMaro's answer wasn't completely clear.
Jonathan Sampson
Good points Jonathan, I should have mentioned those. Thanks
MitMaro
lr_text will only work with the first form but I have several on my pages (just 2 on the given example but I have more) so I tried something with$('#' + this.name + '_text')But it didn't work.
Mark
Please, does anyone have some working code I didn't succeed.
Mark
Thanks a lot for your answer. About validation, I validate my code most of the time, for this code it works with external JavaScript file only. I don't know why it doesn’t when it’s inside html.I didn't get what you mean about including jQuery twice.Because I need to have some jQuery inside body and another inside head.If I put both together it doesn't work.Could you tell me more about your thought?
Mark
You have the jQuery library included in your code once on line 12 directly under the head tag and you have it included again on line 21 just after the title tags.For validation the only problems you have is that you are not including type="text/javascript" to your script tags.
MitMaro
Double jQuery comes from jsbin.com. On my local server I don't have it twice and I have "text/JavaScript" too so it's validated.But thanks for noticing it.I also noticed some small bug on this code: http://jsbin.com/eqece/Steps to the bug:-clicking "French"-typing "blablabla"-clicking "German"-removing "blablabla"-clicking in a blank space (outside of the form and radio buttons)It puts back "French" inside the form box instead of "German"(as it's selected).I tried by clicking several buttons, it only keeps last grey text before typing.Do you have any idea how to solve this bug?
Mark
I fixed the bug you described, it was a simple fix. The title attribute needs to be updated even when the class is not 'hint'.
MitMaro
Thanks that fixed the bug ;-)I updated the code with your fix here: http://jsbin.com/oxeye/I don't want to bother you but I noticed some other bug.Indeed when typing something and doing the query to display search results, if I go back to the search page in my browser (Firefox 3.5 but same thing with IE8) there isn't my search term anymore. It's replaced by the grey text. But when I remove the jquery code, the filled text is still present. Do you have some fix to keep this text if filled? (I may create some other question)
Mark
Create another question. I think this is a browser issue and one I have never dealt with before. Some other people may have some creative solutions for this one. :)
MitMaro
Thanks anyway, I did so here: http://stackoverflow.com/questions/1123032/how-to-keep-some-input-text-removed-by-jquery-when-going-back-with-browser
Mark