Javascript to get sharepoint form field value to a variable - javascript

I want to copy the content of a sharepoint form field to a variable using Javascript.
Eg. I have a field named "Language" in my sharepoint edit form. Now I just want to get the value of that field to a varaible x.
Please help.
BR

It depends on the type (e.g. user, lookup, multilookup, text, note, etc.) of field. I am using jQuery in my custom list forms and the name of the field for any given content type will be added to the id of the corresponding html control with the text 'Field' appended to it. However, like any typical asp.net control, the id of the html form control rendered to the client will reflect its control hierarchy so you have to account for that when searching for a field. anyhow, the following works for me if i need to reference fields in my custom forms. ** notice the +Field which implies that the name of the field is concatenated with 'Field'
var $titleField = $('input[id*=TitleField]');
var $lookupField = $('select[id*=Name_Of_Field+Field]')
var $multiLookUpCandidate = $('select[id*=Name_Of_Field+Field][id*=SelectCandidate]')
var $multiLookUpResult = $('select[id*=Name_Of_Field+Field][id*=SelectResult]')
var $note = $('textarea[id*=Name_Of_Field+Field]');
You can pick up on the trend by viewing source and seaching for your contenttype/sitecolumn field name. You will find it in an html form control id. use that information to learn how to reference other field types.

Without posting your code its very difficult to understand what you want to do.... to get a value from a form you can do the following :
HTML
<form id="myform">
<input id="myinput" type="text" value="something" />
</form>
JavaScript:
var myinputval = document.getElementById("myinput").value;
myinputval would be "something" in this example
Demo : http://jsfiddle.net/Qk6FZ/

This might help:
http://depressedpress.com/2012/09/24/obtaining-a-javascript-reference-to-a-sharepoint-field/
Using that function you'd get the reference using something like:
var LanguageFld = getFieldRef("Language");
Once you have the refernece it's easy to get the value:
var CurValue = LanguageFld.value;
Hope this helps!

Related

How to make multi choose autocomplate and how to send data?

Hi i use w3schools autocomplate tutorial. Link https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_autocomplete
I want to make multi choose. For example ; France and England for Country. Actually i saw multi choose example on Javascript but i need a variables. When i click send button my query executed so i need variables.
How can i do it ? and how can send value from input area to sql query ?
Image
It is difficult to understand what you're looking for in the way you phrased the question, but I will try answer based on how I understood the question.
First, I would add hidden input to contain array values under input ID: #myInput.
Try the following:
<input id="countries" type="hidden" value="countries[]" />
<input type="button" value="add" onclick="addCountry">
As you see, I bind click event to the button so function would be:
function addCountry(){
const countries = document.querySelector('#countries');
const country = document.querySelector("#myInput");
countries.value = [...countries.value, country.value];}
So, when you submit form, you will be able to get this from a PHP file
with []
$countries = $_POST['countries'];
Alternatively you can use $_GET instead.

Unable to assign "form" attribute to "input" tag via Javascript

According to specification input tag has a form attribute, which Specifies the form the input element belongs to.
HTML example:
<form id="putForm" action="index.html" method="post"></form>
<input type="text" id="field" value="foo">
Javascript code:
const putForm = document.querySelector('#putForm');
const field = document.querySelector('#field');
field.form = "putForm";
Attribute is not assigned, shows "null" when inspected with dev tools.
The form property has, for a very long time, been a read-only reference for the form element associated with the input and not a string with the ID of the form in it.
When the form attribute was introduced, the form property was already taken.
Use the setAttribute method instead.
field.setAttribute("form", "putForm");
I was able to accomplish this with the following code:
var input = document.getElementById('field');
input.attributes.form.value = "putForm";
why don't you just do
$("#field").val('anything');
and you are done

How to retrieve data from database using jQuery and information in a form from a previous page?

So I am trying to get a user's first name so that I can display it on my chatting app. I have the user's information. I can get the user by playing around with res.render(). In jade I can do something like #{user.firstName} to have it printed out but I am not sure how I can use this data in jQuery.
Check if you can build an element as follwed:
case 1
<input id='user-first_name' type='hidden' value='#{user.firstName}' />
// Then you can use the value of input field as.
var firstName = $('#user-first_name').val()
case 2
you can directly write a script tag where you will initialize some window variables as :
<script>
window.user_first_name = #{user.firstName}
</script>
case 3
Also you can try assigning that value to data field of any html element(html tag) as followed :
<element data-first_name='#{user.firstName}'>
// Now you can use a jquery selector for selecting that element and you
// will be able to retrieve that value using data function
var firstName = $(element_selector).data('first_name');

Insert the value retrieved from localstorage to the input field

I am using localStorage where in I am able to store in previous page and retrive it in this page. (Checked it using alert).
name12=localStorage.getItem("content");
Now my requirement is to display it into the input field and make it non-editable.
Please help me out with it. I have tried different things but I am not able to get it right.
Used onblur="localStorage.setItem(this.name, this.value) in the input tag
and also tried to use
if name_2.value = name12; in script tag
To make a field uneditable, you can use the html attribute disabled on the input field. http://www.w3schools.com/tags/att_input_disabled.asp
To set a default value for a field, you can use the html attribute value. In your case since the value is dynamic, you probably want do not want to do that inline in the html. One possible solution is to set the value attribute through javascript like below.
<script type="text/javascript">
var name2 = document.getElementById("name_2");
name2.value = localStorage.getItem("content");;
</script>
Set the value of an input field
You have to assign the value to input using javascript.
<input username" id="name_2" type="text" placeholder="name" name="name_2" required="required" autofocus="autofocus">
<script>
var n = "5"; // read from storage here
var inpt = document.getElementById("name_2");
inpt.value = n;
inpt.disabled = true;
</script>
There are many places to find this information. Try reading the jquery documentation when you get stuck. For example, here is a page that describes how to set the value of an input element. StackOverflow also has many questions about this topic. A quick google search brings up this question about setting the value of input elements. We can also find SO questions about disabling input elements easily, like this one.
I encourage that you attempt to use more resources to find what you need before bringing your questions here.
Based on the answers to the questions I linked, we can set the input and then disable it to keep the value from changing:
$('#input').val(name12);
$('#input').prop('disabled', true);

Copy my addresses in different fields in javascript

i have in my jsp different addresses, I can choose to copy to my fields. I wanted to know if anyone has sample code in javascript to provide me, thank you.
http://www.freeimagehosting.net/image.php?87514f7c3e.jpg
Different editable fields . A list of address, when I select an address, I want to automatically informs my fields
I think it's time to learn basic JavaScript/HTML DOM :) Here are some tutorials:
W3Schools JavaScript tutorial
W3Schools HTML DOM tutorial
Essential JavaScript - tutorial
To start, you need to give HTML elements of interest an ID:
<input type="text" id="address">
<input type="text" id="otherAddress">
You can grab a specific element using document.getElementById():
var addressElement = document.getElementById('address');
var otherAddressElement = document.getElementById('otherAddress');
You can get the value by value attribute:
var addressValue = addressElement.value;
You can set the value by the same attribute:
otherAddressElement.value = addressValue;

Categories