0

I currently have an html file named abc.html which contains two input elements

<input type="text" id="txt"></input>
<input type="button" id="btn"></input>

The button simply displays the textbox value.

  1. Is there any way that I can add some url parameter so that I can specify the value of textbox.

  2. Can the button onclick be automatically initiated when the page loads.

Inshort I want to give url parameters such that when I open the url I just see the value that I passed in the parameter. Button onclick event should also be initiated.

Lucky
  • 16,787
  • 19
  • 117
  • 151
eliya
  • 99
  • 7
  • Did you similar to google [search page](https://www.google.co.in/search?q=auto+text) ? –  May 05 '15 at 08:02
  • can you make a jsfiddle of what you have done till now? – Lucky May 05 '15 at 08:05
  • Maybe [HERE](https://docs.djangoproject.com/en/dev/topics/http/urls/#captured-parameters) – Trix May 05 '15 at 08:31
  • trigger event: use fireEvent or dispatchEvent. As for getting HTML URL parameters, refer to https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/2880929#2880929 – Howard Wang May 05 '15 at 08:41

2 Answers2

0

Is there any way that I can add some url parameter so that I can specify the value of textbox.

function getURLParameter(name) {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
}

Now to get url parameter

var param = getURLParameter("paramName"); 

To set value of textbox

var elem = document.getElementById("txt");
elem.value = param;

Can the button onclick be automatically initiated when the page loads

window.onload = init;
function init() {
var button = document.getElementById("btn");
button.onclick = handleButtonClick;
}
function handleButtonClick() {
alert("Button has been clicked");
}

Hope it helps. Let me know if you have any confusion.

mirmdasif
  • 6,014
  • 2
  • 22
  • 28
0

You will use this code to click button automatically.

$(document).ready(function(){
   document.getElementsById("btn")[0].click();
});
Lakhan
  • 12,328
  • 3
  • 19
  • 28