views:

43

answers:

1

How do i focus a input type="text" automatic when my id changes in page url ?

For example: http://mysite.php?id=2
I want to focus the mouse on my type="text" when my PHP IF statement is met without using buttons.

<form>
<input type="text">
</form>

It should go something like: php IF (condition) { focus the mouse to input type="text" based on id of url }

+1  A: 

You mean this:

<?php
  if (intval($_GET['id']) === 1) // adjust value
  {
?>

<script type="text/javascript">
  // focus element
  var el = document.getElementById('elem_id');
  el.focus();
</script>

<?php } ?>
Sarfraz
I know many ways to do this as you have done it. This solution is good but it ain't practical.I have as many type="text" inputs as there are ids. That means that each input has the same id name (elem_id) as the id number grows. I need to give each input type="text" its number that is the same as id number of url.input type="text" id="elem_id(some code that tells that the number is the same as $_GET['id'])"
cincg
@cincg: The problem is that you are using same id for multiple elements. An id should be used once per element per document.
Sarfraz
Well yes, that is the problem that needs to be solved. You surely don't suggest that i write this code for each id if there are 3000 ids. That would mean also that i would need to create 3000 inputs and give each its id name also.
cincg