0

I have an HTML with say a textfield (input element).

<input name="capacity" type="text" value="blah blah blah">

This simply displays a text field on my page with a default value "blah blah blah".

What I want to do is remove value attribute, as I don't want to see this default value. I am doing this using javascript.

value = element.getAttribute("value");
    if((element.readOnly != undefined || element.readOnly == false) || (element.disabled != undefined || element.disabled == false)){
    //element.removeAttribute(value);    
    element.removeAttribute("value");    

But it is not working. I even tried

element.setAttribute("value","");

but no luck.
Any pointers where I may be missing.


EDIT :
I got an issue related to this question, anyone interested may check this

*********************************************

Thanks a lot.

2
  • Have you considered using a placeholder attribute instead of a default value? Only works in modern browsers though. Commented Sep 27, 2011 at 6:41
  • @Chamika Already done that...See 3rd comment for alex's answer.Thanks for replying, though. Commented Sep 27, 2011 at 8:42

3 Answers 3

3

...I don't want to see this default value.

Just set the value property directly to an empty string.

document.getElementsByName('capacity')[0].value = '';

jsFiddle.

Sign up to request clarification or add additional context in comments.

6 Comments

by directly you mean, simply open the HTML into edit mode instead of browser and change the attribute manually.
@mohit That's now what I meant, but if you could do that, why bother with JavaScript at all? What outputs this form element?
It worked . I was too worked up with removeAttribute and setAttribute that I totally missed this simple friend of mine. :) Thanx alex. I modified it a bit though. element.value=""; This way I don't need to care about id or name of my elements.
It seems that the values are not being changed permanently. I mean as I refresh the page it starts showing the old values again.Any idea what is going on ?
@mohit That's expected behaviour. HTTP is a stateless protocol by design.
|
0

Give your text field and id like <input name="capacity" type="text" id="text" value="blah blah blah"> document.getElementById['text'].value = "";

Comments

0

This is your html tag. You will need to add a ID to it

<input id="capacity" name="capacity" type="text" value="blah blah blah">

This is just to fire the javascript function

<input type="submit" value="Click" onclick="javascript:return reset();" />

The following function will reset the value of the selected element

<script type="text/javascript" language="javascript">
    function reset() {
        document.getElementById("capacity").value = "";
        return false; // In order to avoid postback
    }   
</script>

If you are not using form and you want to use it with just the name you can try the following

 this.capacity.value = '';

3 Comments

+1 for very nicely written answer but capacity is not id, it is name.Will your code work ?
It's pretty awful really. The language attribute was deprecated over a decade ago, there is no need for "javascript:" in the handler, it is treated as a useless label, returning false from the submit button's click handler will not stop a form submitting. Don't use a submit button if you don't want it to submit the form (change the type to button), etc. The handler could be: this.form.capacity.value = '';
Thanks buddy :-), will remember that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.