EMM
EMM

Reputation: 1812

How to remove attributes from HTML using javascript?

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.

Upvotes: 0

Views: 3078

Answers (3)

Nitin Walawalkar
Nitin Walawalkar

Reputation: 39

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 = '';

Upvotes: 0

Umer Hayat
Umer Hayat

Reputation: 2001

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

Upvotes: 0

alex
alex

Reputation: 490143

...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.

Upvotes: 3

Related Questions