views:

360

answers:

4

How can I extract the value attribute of an input tag? Using SIMPLE HTML DOM let me give you an example:

<form action="#" method="post" name="test_form" id="test_form">
Name<input type="text" name="name" value="NaMe"/><br />

Address<input type="text" name="address" value="AdDrEsS"/><br />

<input type="hidden" value="sayantest" />
</form>

I want to extract just the value of hidden type input tag, not the others.

+3  A: 

You want to put the id (so you can access the value in javascript), as well as a name (if you want to access the value on the server) in the tag you wish to get the value from.

e.g.

<input type="hidden" name="test" id="test" value="sayantest" />

then your javascript is as simple as:

<script type="text/javascript">
  var val = document.getElementById('test').value;
  alert(val);
</script>
Kris Erickson
+1 You can also consider making use of the jQuery library and accessing your elements using that. If you do use jQuery, your code may look something like this $('#test').val();
Sonny Boy
Check out more basics at: http://www.w3schools.com/jsref/met_doc_getelementbyid.asp
J Angwenyi
A: 

The easiest way, as already mentioned, is to give your hidden input an id attribute and then use getElementById and then .value or .getAttribute('value') to select it.

Alternatively, if you want to get the values of all hidden inputs on the page, or can't inject your ID, you could use something like this:

var inputs = document.getElementsByTagName('input');
for(var i = 0; i < inputs.length; i++){
    if(inputs[i].getAttribute('type') == 'hidden'){
     alert(inputs[i].getAttribute('value'));
    }
}
Rudism
A: 

but kris, i wanna do it with SIMPLE HTML DOM library.. NO javascript :(

Sam
+2  A: 

using SIMPLE HTML DOM

Do you mean the PHP library of that name?

If so, you'd have to choose a way to identify the input. If you can't change the markup to add an id or name on the hidden input you want, you'd have to come up with something like “get the first input with type hidden in the form”:

$html= new simple_html_dom();
$html->load('<html><body<form action="#" method="post" name="test_form" id="test_form">Name<input type="text" name="name" value="NaMe"/><br />Address<input type="text" name="address" value="AdDrEsS"/><br /><input type="hidden" value="sayantest" /></form></body></html>');

$input= $html->find('#test_form input[type=hidden]', 0);
$input->value;
bobince