开发者

How to remove attributes from HTML using javascript?

开发者 https://www.devze.com 2023-04-08 02:48 出处:网络
I have an HTML with say a textfield (input element). <input name=\"capacity\" type=\"text\" value=\"blah blah blah\">

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 "开发者_JAVA技巧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.


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消