Home > Mobile >  Javascript or jquery change fields value by name
Javascript or jquery change fields value by name

Time:02-10

I use these codes to change the value of a field. Only the first two lines run, I tried the rest of the code but it did not work and sometimes the message "Uncaught ReferenceError: $ is not defined" is displayed on the console.

Error displayed on console

document.getElementsByName('tmcp_textfield_6').value = Pak;
document.getElementById("tmcp_textfield_116202d52cbd078").value = Pak;
$("input[name='tmcp_textfield_6']").val(Pak);
document.getElementsByName("tmcp_textfield_6").value = 100;
document.getElementByname("tmcp_textfield_6").value = "100";
$("input[name='tmcp_textfield_6']").val(Pak); 

CodePudding user response:

Find the Working Script here, I think you should switch quotes, where double quotes use single, and where single quotes, use double.

var Pak = "Test"
$('input[name="tmcp_textfield_6"]').val(Pak)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="tmcp_textfield_6">

Be sure you've also included the jquery file to whatever page you need it to work on

  • Related