Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to send html id attribute to a function?
    primarykey
    data
    text
    <p>My code is below.I want to send id=content to the function mr. And then write result to the passed id=result.Although it is only for this html file,I want to make this function available for another html pages, and want to add this function in another util.js file.</p> <pre><code>Add Item: &lt;input type="text" name="name" id="content"&gt; &lt;br&gt; &lt;button onclick="javascript:mr('POST',content,result,'post.php');" type="button" id="btn1"&gt; Submit &lt;/button&gt; &lt;br&gt; &lt;button onclick="javascript:mr('GET',content,result,'get.php');" type="button" id="btn2" &gt; List Jobs &lt;/button&gt; &lt;div id="result"&gt;&lt;/div&gt; </code></pre> <p>The function mr is like this.It is for ajax post and get operations:</p> <pre><code>function mr(type,content,result,URL) { var hr = new XMLHttpRequest(); //var content = document.getElementById("content").value; var vars = "content=" +content; if (type == 'GET') URL = URL + '?' + vars; hr.onreadystatechange = function() { if (hr.readyState == 4 &amp;&amp; hr.status == 200) { var return_data = hr.responseText; document.getElementById("result").innerHTML = return_data; } } hr.open(type, URL, true); hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); switch(type) { case 'GET': hr.send(); break; case 'POST': hr.send(vars); break; } document.getElementById("result").innerHTML = "Processing..."; } &lt;/script&gt; </code></pre> <p>post.php</p> <pre><code>&lt;?php echo "POST\n"; if(isset($_POST)) print_r($_POST); ?&gt; </code></pre> <p>get.php</p> <pre><code>&lt;?php echo "GET\n"; if(isset($_GET)) print_r($_GET); ?&gt; </code></pre> <p>When I click submit ,I got the following output.</p> <pre><code>POST Array ( [content] =&gt; [object HTMLInputElement] ) </code></pre> <p>And clicking list button the output:</p> <pre><code>GET Array ( [content] =&gt; [object HTMLInputElement] ) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload