I have some input fields with name="something" and value=""
I would like to replace value="" with value="something"
How can I do it with regex? Actually, can I do that with regex?
Perhaps something in Javascript? I would grab the input name and put it into the value, quick and dirty example here: http://jsfiddle.net/c3tdLLyL/1/
var newValue = document.getElementsByName("inputname")[0].name
document.getElementsByName("inputname")[0].value = newValue
Why not use jQuery using .val()
if you want to set it after the page has loaded or just PHP if you want to set it dynamically before the page has loaded?
If this markup comes from PHP, I'd suggest use an HTML Parser for this task. DOMDocument in particular:
$html_markup = '
<form method="POST">
<input type="text" name="something" value="" />
</form>
';
$dom = new DOMDocument();
$dom->loadHTML($html_markup);
$xpath = new DOMXpath($dom);
$out = '';
$input = $xpath->query('//input[@name="something"]'); // target that particular element
if($input->length > 0) { // if found!
$input = $input->item(0);
$input->setAttribute('value', 'something'); // set your value
foreach($dom->getElementsByTagName('body')->item(0)->childNodes as $e) {
$out .= $dom->saveHTML($e);
}
echo $out;
}