1

Suppose I have three kind of HTML coming either of them:

<input name="myName" id="myID" role="textbox" style="width: 98%;" type="text" value="SDFSF"/>

OR

<input name="myName" id="myID" role="textbox" style="width: 98%;" type="text" value='SDFSF'/>

OR

<input name="myName" id="myID" role="textbox" style="width: 98%;" type="text"/>

I don't know which of them coming. I am trying to edit them like this:

<input name="myName" id="myID" role="textbox" style="width: 98%;" type="text" value=''/>

I am using this thing:

html = html.replace(/value="(?:[^\\"]+|\\.)*"/,"value=''");

This is able to replace value="Something" to value=''. How can I extend this for other two? Is there any option for OR in Javascript?

Edit

I don't have the DOM element. So, I have to use it like this.

6
  • 8
    Why are you using regular expressions, when you could use the DOM to do this more easily and more reliably? Commented Jun 18, 2013 at 9:26
  • 5
    Why, oh why?! You already have HTML - just use the DOM to manipulate it! document.getElementById("myID").value = "" Commented Jun 18, 2013 at 9:27
  • 1
    @Aleks: getElementById only works tough if the element is part of the document. If the OP gets the data as HTML string, that won't work. There are, however, other and equally easy ways. Commented Jun 18, 2013 at 9:29
  • I have the HTML only. I don't have the DOM element when I am replacing it. Commented Jun 18, 2013 at 9:31
  • @FelixKling I highly doubt that the OP is getting HTML as strings to be processed with javascript. Commented Jun 18, 2013 at 9:31

2 Answers 2

5

Parse the HTML into a DOM element and manipulate it. Then you can convert it back to HTML or whatever you want to do with it:

var container = document.createElement('div');
container.innerHTML = html;
container.firstChild.setAttribute('value', '');

html = container.innerHTML;

Note: Others mentioned to use element.value = '';. This works as long as you don't want to serialize the element back to HTML. If you did, it would still have the original value value.

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

Comments

1

Please use simply the DOM

document.getElementById("myId").value = "";

you can also refer to the previous value to check.

var el = document.getElementById("myId")
var myValue = el.value;

if (myValue === notSoGood) {
    el.value = "BetterValue";
}

Comments

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.