2

I have a Sharepoint custom list where the users can add new items. I have 2 textbox ("A" & "B") and I would like to insert a value to "B" if "A" is changed. Problems:

1. How to select a textbox?

To begin I tryed to simply insert a text to textbox "A" based on this, but I was not successful. Here is my code:

<script type="text/javascript">
$(function () {
$('input[name="Test"]').val('some text');
 });
</script>

If I use chrome, press F12 and select textbox "A" it says the ID is "input#Test_cc4abb3b-4c1c-45a4-83fc-af4a2dcb1d99_$TextField.ms-long.ms-spellcheck-true". So I tryed:

$(function () {
$('input#Test_cc4abb3b-4c1c-45a4-83fc-af4a2dcb1d99_$TextField.ms-long.ms-spellcheck-true').val('some value');
});

But sadly still not works. Maybe I am missing somthing else. I belive this should fill the selected textbox by default when I open the page.

2. How to make it based on an chage event?

I would like to trigger the set textbox "B" if the user finished writing in textbox "A". (pressing enter/tab or clicking out from the field ect...)

3. How can I insert to the textbox "B" from an other list?

For example: I would like to insert to textbox "B" from the unique column called "ID", based on the unique column "Name" where I get the "Name" value from the textbox "A"(the user write it). (Both "ID" and "Name" are columns of the other list.)

Sorry for the trivial questions, I am new to Jquery and thank you very much for your guidance!

Nefri
  • 197
  • 2
  • 13

1 Answers1

1

If you use jQuery you can just bind an onchange event handler to input A that changes the value of input B.

$(function() {
    $("input[name='A']").on('change', function() {
    $("input[name='B']").val( ($("input[name='A']").val() * 2) );
  })
});

Example fiddle: https://jsfiddle.net/3rh914ru/

Edit:

So what you need is something like this;

$("*[name*='Test_']")

Using a selector like this, you can select all elements containing the string 'Test_' in their field. According to this i updated the fiddle like this;

$(function() {
    $("*[name*='Test_']").on('change', function() {
    $("*[name*='Result_']").val( ($("*[name*='Test_']").val() * 2) );
  })
});

Updated fiddle: https://jsfiddle.net/3rh914ru/1/

Ugur Ilter
  • 92
  • 3
  • Thank you! This should work for the event part. But looks like I have "uncaught reference error $ is not defined". Even if I defined the name correctly, probably this is the root of my problem. I guess sharepoint automaticly generate a quite complex path to the textbox so it is not works if I simply refer to it's name/title/id. So do u have any tips how to get the reference of my textbox? Even if I try to use: document.getElementById("Test_cc4abb3b-4c1c-45a4-83fc-af4a2dcb1d99_$TextField").value = "My value"; I get the same error. I tryed many way to refer, but I can find the correct one. – Nefri Sep 26 '17 at 15:39
  • Please see my edit and let me know if you need further help :) Good luck ! – Ugur Ilter Sep 26 '17 at 20:19
  • Thank you very much, finaly I could get the ID thanks to your "*=" method. :) – Nefri Sep 27 '17 at 09:08