How to "document.write();" on the same page? - document

Basically, I have made a form which allows you to input 2 numbers, and when you press the 'Add' button, the program writes the answer onto the screen, the only thing is when the answer is written, it appears on a separate page. How do I get it to write to the same page, below is the HTML code:
<form type="twoNum" method="get">
<input type="float" placeholder="Enter first number here..." name="num1" id="n1"/>
<input type="float" placeholder="Enter second number here..." name="num2" id="n2"/>
<input type="button" value="Add" name="sndfunct" onClick="twoNum(this.form);"/>
</form>
Below is the Javascript code:
function twoNum(form)
{
var num1 = form.num1.value;
var num2 = form.num2.value;
var intNum1 = parseFloat(num1);
var intNum2 = parseFloat(num2);
document.writeln(intNum1 + intNum2);
}

Please note that using document.write() is considered bad practice. E.g. see the warning on the W3C web site.
Furthermore, you can’t use it to edit a closed document. document.write() can only be used while the document is being loaded.
In order to do what you want, you should have a <span id="foo"></span> somewhere in your document, and then do:
document.getElementById("foo").textContent = intNum1 + intNum2;
This will insert your number inside your span element. Actually, it replaces the content of the (previously empty) span element.
Edit: Of course, it can be any kind of element. I used a span element just for the example.

You can write <p id="answer"></p> in the form. Then create var answer=intNum1 + intNum2 and after that instead document.writeln(intNum1 + intNum2) write document.getElementById("answer").innerHTML=answer!

Related

VBA how to fill in search bar with ID?

Web page has the following HTML without an ID - how do I set the value of the form?
<input name="loanxFindBorrower" onkeypress="if((window.event&&window.event.keyCode==13) ||
(event.which&&event.which==13 )){ findByBorrowerAction()}" type="text" size="25" value="">
I've tried
IE.Document.getElementByTagName("loanxFindBorrower").Value = "New Value"
Aswell as
IE.Document.getElementByTagName("loanxFindBorrower").Focus
IE.Document.getElementByTagName("loanxFindBorrower").Value = "NewValue"
Any help would be much appreciated
You should use getElementsByName() to get the element. It returns a collection, you'll need to specify the index to get the element you want.
For example, if it's the first element with name "loanxFindBorrower" in the page, the index is 0:
IE.Document.getElementsByName("loanxFindBorrower")(0).Value = "New Value"

xpath to get checkbox inside label

I have this code
<label>
<input type="radio" checked="checked" value="fOejPdlZIx83HA" name="btnRad">
Test1
</label>
<label>
<input type="radio" checked="checked" value="fdsaf4waff4sssd" name="btnRad">
Test2
</label>
<label>
<input type="radio" checked="checked" value="fg43fasd43wsat4" name="btnRad">
Test3
</label>
I wish to access the radio button depending on the label text via xpath
I already tried multiple thing:
//input[#name='btnRad]']/following::*[contains(text(),'Test3')]
//label[text()='Test3']/input[#name='btnRad']
//*[contains(text(),'Test3')]
Even the last one return me nothing, so xpath think that "Test3" is not the text of the label... anyone have an idea how to do this?
Your expression is failing because your label has more that one text node: an empty string before the input, and Test3. The way you're using contains means it will only check the first text node, ie empty string.
Two ways of solving this:
eliminating the empty strings with normalize-space():
//*[contains(text()[normalize-space()], 'Test3')]
querying each text():
//*[text()[contains(.,'Test3')]]
For a more detailed explanation, see How to search for content in XPath in multiline text using Python?.
This works also //label[contains(.,'Test3')]/input .

setting textarea text based on current input field

I have two input fields
input(ng-model='form.firstName', name='firstName', id='familyName')
and
input(ng-model='form.lastName', name='lastName')
I also have a textarea field
textarea(id='fieldInfo', ng-model='fieldInfo', name='fieldInfo' cols='15', rows='10')
I would like to change the text in the TextArea to "Please enter your first name" when the focus or cursor is in the First Name input and change it to "Please enter your last name" when the is on the last name inout and it should be able to toggle as the user switches focus from first name to last name and last name to first name.
Any assistance will be much appreciated.
Melroy
The ng-focus directive will not work for you if you are using one of the stable 1.0.x versions of AngularJS. However, I have created a working CodePen example of how you could do it without the ng-focus directive.
I created my own on-focus directive that evaluates an expression, which results in an update to the scope model being used by the textarea. I'm sure it could use some refinement, but the concept should still work for your application.
You can use the ngFocus directive if you are using angular 1.2.0.
In your controller:
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', ['$scope', function($scope) {
$scope.text = "";
}]);
And in your view:
<input type="text" data-ng-model="form.name" ng-focus="text = 'Please enter your first name'" />
<input type="text" data-ng-model="form.last" ng-focus="text = 'Please enter your last name'" />
<textarea name="area" id="textarea" cols="30" rows="10"> {{ text }} </textarea>
You can check this fiddle
Edit: Wrong fiddle link

Is there anyway to pass parameter from snippet to chunk

I am learning modx and stuck at a point. I want to post "uid" value to next page via POST only and want to set hidden field which will contain "uid" as a value. I believe practically its not allowed to pass values to chunk.
I want to know whats the proper way so i can get POST data and use that value in chunk ??
My procedure
1) I have created resource (document) which contains call to snippet and then chunk
2) snippet contains value retrieved from POST
3) chunk contains a form and fields, I want to inject that POST value in this form.
There are several ways you could do this.
1) Return the uid value directly from the snippet (let's call it getPostData), and place the snippet call in your hidden field in the chunk like this:
<input type="hidden" name="uid" value="[[!getPostData]]" />
Note the snippet is uncached ([[! opening tag) otherwise the first form submission will be cached.
2) Place the snippet call in the chunk tag and have the value passed into a placeholder:
[[$myChunk?uid=`[[!getPostData]]`]]
...and in your chunk set an uncached placeholder for 'uid':
<input type="hidden" name="uid" value="[[!+uid]]" />
3) Recommended: Use setPlaceholders() in your snippet to output content to placeholders anywhere in your page - you can use it to output to multiple placeholders:
<?php
// please sanitise your POST values, this is just an example
$placeholders = array();
$placeholders['uid'] = $_POST['uid'];
$placeholders['email'] = $_POST['email'];
$modx->setPlaceholders($placeholders);
...and your chunk:
<input type="hidden" name="uid" value="[[!+uid]]" />
<input type="email" name="email" value="[[!+email]]" />
Documentation: http://rtfm.modx.com/display/revolution20/modX.setPlaceholders

A script to convert text in a form to query?

I'm a total newbie and want to start with php. I know some javascript already.
I want to be able to type some text in a form and convert it to a query e.g.
In my website there's this search box, I type in 'example' click submit and it gives me this=
http://www.externalsite.com/search?s=example&x=0
and pastes it to the address bar, you know like a search engine.
Any guidance will be appreciated.
Well, as you are doing PHP, you should point your form to submit to a PHP file. Then to retrieve the data, use $_GET or $_POST depending your form is posting or getting (as I can see from your exemple its a GET) so something like this :
HTML :
<form method="get" action="search.php">
<input type="text" name="q" id="q" value="" />
<input type="submit" value="Submit" />
</form>
On the PHP side :
<?php
$query = $_GET['q'];
header('Location: google.com/search?q=' . $query . '%20term');
die();
?>
Basically you're typing your search term into a form, which then posts (via GET) to a search page, which queries its database for records matching that string. A simple example of this follows:
index.php
<form method="get" action="search.php">
<p><input type="text" name="terms" /></p>
<p><input type="submit" value="Search" /></p>
</form>
When you submit that, it will direct you to search.php?terms=[terms here]. Our code found within search.php follows:
search.php
mysql_connect($host, $user, $pass) or die(mysql_error());
$terms = $_GET["terms"]; // you'll want to sanitize this data before using
$query = "SELECT col1, col2, col3
FROM tablename
WHERE col1 LIKE '%{$terms}%'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
print "We've found results.";
} else {
print "No results found.";
}
This is a very simple example (don't copy/paste this into production). Essentially you're pulling the submitted value(s) into a query, and then showing any results. This should be enough to get you started, but feel free to visit us here if/when you have more specific questions in the future.
Best of luck!

Resources