Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

cannot clear default input values

Options
  • 07-12-2010 1:21am
    #1
    Registered Users Posts: 4,222 ✭✭✭


    Hi,

    I am setting default values for a number of input box for when a search is posted, the default value is what was used to do the search for that particular input.
    If the user deletes the contents on an input box it doesnt stay deleted, it returns to the default value. They can change it fine but not delete the contents totally.
    //input example:
    <div class="row">
      <label for="begin">Begin With</label>
      <input type="text" class="txt" id="begin" name="begin"  value="{$value}" onchange="changeTest(this.id);" />
    </div>
    
    //this wont allow an empty value either!!
    <input type="text" class="txt" id="test" name="test"  value="TEST" onchange="changeTest(this.id);" />
    
    //javascript function example :
    function changeTest(id){
      var foo = document.getElementById(id).value;
      if (foo == ''){
        document.getElementById(id).value = '';
      }
    }
    

    the only hack around it is to set the value as a single space and trim the posted value which inst what i want to go with.
    seems simple but has me flummoxed as to why it aint working!


Comments

  • Registered Users Posts: 171 ✭✭conorcan2


    [PHP]
    var foo = document.getElementById(id).value;
    if (foo == ''){
    document.getElementById(id).value = '';
    }[/PHP]

    That doesn't do anything.

    Same as saying:

    If document.getElementById(id).value = ''
    then
    document.getElementById(id).value = ''


    What do you get if you add:

    Alert(document.getElementById(id).value);


  • Registered Users Posts: 4,222 ✭✭✭Scruff


    i know, i was just trying to explictly set it in my desperation.
    Alert box will say its empty but then the input box goes back to showing the default value again.


  • Registered Users Posts: 171 ✭✭conorcan2


    What happens if you don't make a call to a function, but embed the clearing code in the onChange handler itself?

    Also, the following should return the current text in the input box, if you append letters to it (e.g. start typing 'testingmyinputbox')

    [PHP]

    var foo = document.getElementById(id).value;
    Alert(foo);
    [/PHP]

    If it shows an empty string then your reference is incorrect for some reason, it should show the current value of the input field.


  • Registered Users Posts: 981 ✭✭✭fasty


    I'm new enough to Javascript, but does "this.<attribute name>" work? I thought you need to use
    this.getAttribute("id")
    

    Edit: Just tried and apparently your way does work! Pretty cool.


  • Registered Users Posts: 171 ✭✭conorcan2


    fasty wrote: »
    I'm new enough to Javascript, but does "this.<attribute name>" work? I thought you need to use
    this.getAttribute("id")
    
    Edit: Just tried and apparently your way does work! Pretty cool.

    I'm not sure if it works, but try this in the input field definition:

    onChange = "Alert(this.getAttribute("id").value))"

    Hopefully it will be showing you the current text in the input field when you try to edit it.

    Then try
    onChange = "this.getAttribute("id").value = 'should be empty'"

    and if that works try:

    onChange = "this.getAttribute("id").value = '' "

    Be careful with your double quotes: "
    and your single quotes: '

    Edit: just read your edit!


  • Advertisement
Advertisement