Use one line javascript if in html snippet [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This fieldHTML variable is used for creating dynamic field when user press 'add field' button. I'm saving these dynamic fields values in localStorage. After refreshing the page I want to check if this input has saved value and if yes, display this input. Problem is that with this one line if statement which is defined in value attribute my input don't showing even it has value. What's wrong with the line where I defining input value? I believe it's the problem with quotes, because when I try to debug this one line if in console.log I'm getting text value empty. I'm not getting any error in console.
var fieldHTML = '<div> \
<input type="text" name="name[]" class="form-control" placeholder="Enter name" \
value="'+(localStorage.getItem(array['name'][i])) ? localStorage.getItem(array['name'][i]) : 'empty' +'" /></div>';

\ Multiline support is limited to browsers supporting ES5 only.
'empty' +'" Unexpected string concatenation of literals.
Put the if to a frame with brackets ((localStorage.getItem(array['name'][i])) ? localStorage.getItem(array['name'][i]) : 'empty')
Modified script:
var fieldHTML = '<div><input type="text" name="name[]" class="form-control" placeholder="Enter name" value="'+ ((localStorage.getItem(array['name'][i])) ? localStorage.getItem(array['name'][i]) : 'empty') + '" /></div>';

Related

How to copy HTML content into a new tab [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
I am building a web app and I need to copy an HTML div and everything inside it into a new tab. I am writing this code in JavaScript. So far I have been working on creating a blob and then creating a file to download. This approach copies all the HTML code I need BUT it does not copy the text that has been inputted into any text fields. As I understand, the problem is that the entered text is not part of the HTML code so it cannot be copied. However I cannot find another approach to this, please assist. Thank you
You can use Element.setAttribute() on those elements before serializing your DOM tree. For example:
input.setAttribute("value", input.value)
This will copy the mutable value (not serializable) to the attribute (which is serializable).
Here's a snippet demonstration:
const input = document.querySelector('input');
console.log('initial:', input.outerHTML);
input.value = 'hello world';
console.log('before:', input.outerHTML);
input.setAttribute('value', input.value);
console.log('after:', input.outerHTML);
<input type="text">
(Inspired by comment by #vanowm)
First, copy the html (say inside a DIV)
Then you'll need loop through all input fields and read their value so you can apply them.
So the code is like this (amend to suit your further needs):
<div id=container><html>
<input type=text name=text1 id="text1">
<br><input type=text name=text2 id="text2">
</html></div>
<textarea id=target style="width:100%;height:200px;"></textarea>
<input type=button name=go onclick="trigger();" value="Parse">
<script>
function trigger(){
target0=document.getElementById('container').innerHTML;
text1= document.getElementById("text1").value;
text2= document.getElementById("text2").value;
target0=target0.replace('id="text1"', 'id="text1" value="' + text1 + '"');
target0=target0.replace('id="text2"', 'id="text2" value="' + text2 + '"');
document.getElementById("target").innerHTML=target0;
}
</script>

Why I can't get this element identified by an id attribute? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have this element, it is auto generated by mcssl checkout form. It is a custom field. I'm trying to select it using javascript like so:
var form_field_gclid = document.getElementById("#ctl00_ctl00_mainContent_scPageContent_customFieldsControl_customFieldsRepeater_ctl00_customFieldTextBox");;
console.log(form_field_gclid);
<input name="ctl00$ctl00$mainContent$scPageContent$customFieldsControl$customFieldsRepeater$ctl00$customFieldTextBox" type="text" maxlength="200" size="50" id="ctl00_ctl00_mainContent_scPageContent_customFieldsControl_customFieldsRepeater_ctl00_customFieldTextBox" class="text">
But I'm getting null as a result. I've tried also, document.querySelectorAll(...); but the same result. It's working when I tried it from console but I'm wondering why it won't work if it's on page javascript. Any ideas would be appreciated. Thank you.
I tried getting rid of the # sign but same result.
<script type="text/javascript">
(function() {
var form_field_test = document.getElementById("ctl00_ctl00_mainContent_scPageContent_customFieldsControl_customFieldsRepeater_ctl00_customFieldTextBox");;
console.log(form_field_test);
}());
</script>
This is the full script I'm using.
You do not need the # in your call to document.getElementById. Simply remove it.
var form_field_gclid = document.getElementById("ctl00_ctl00_mainContent_scPageContent_customFieldsControl_customFieldsRepeater_ctl00_customFieldTextBox");
If you were using jQuery, however, you would need it:
var myElement = $('#myElementId');
But since you are using vanilla JS, simply pass in the element's id as a string.
You have to put the script below the html of the input you are trying to hook.
If the form is not rendered the script will return null.
In your webpage you run the script before the input form is rendered.
I think you are looking for the input value. Right?
Also i added a button for you to give you an example about how to add more functionality. For example, how to add a background color to your input
var form_field_gclid = document.getElementById("ctl00_ctl00_mainContent_scPageContent_customFieldsControl_customFieldsRepeater_ctl00_customFieldTextBox").value;
console.log(form_field_gclid);
// add color to your input
function addColor(){
form_field_gclid = document.getElementById("ctl00_ctl00_mainContent_scPageContent_customFieldsControl_customFieldsRepeater_ctl00_customFieldTextBox").style.backgroundColor = "green";
}
If you mean to get the value of the input, i think you are looking for this:
<input name="ctl00$ctl00$mainContent$scPageContent$customFieldsControl$customFieldsRepeater$ctl00$customFieldTextBox" type="text" maxlength="200" size="50" id="ctl00_ctl00_mainContent_scPageContent_customFieldsControl_customFieldsRepeater_ctl00_customFieldTextBox" class="text" value="1">
<button onclick="addColor();">change color</button>
You could try this old school vanilla ::
var form_field_gclid = ctl00_ctl00_mainContent_scPageContent_customFieldsControl_customFieldsRepeater_ctl00_customFieldTextBox;
console.log( form_field_gclid );
<input type="text" maxlength="200" size="50" id="ctl00_ctl00_mainContent_scPageContent_customFieldsControl_customFieldsRepeater_ctl00_customFieldTextBox" class="text">

I need to populate the value of a text field [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The value of universal_leadid is populated on every refresh and it come from a script related to a 3rd party Lead verification service (leadid.com). The objective for me is to post the other6 field value the same as the value assosiated with universal_leadid field.
HTML:
<input type="hidden" name="Other6" value="">
<input type="hidden" name="universal_leadid" value="KFLS00-D254-DD521-GF52-GHT554125" id="leadid_token">
Lead Id value is populated by lead id but I can't get value of the Other6 field.
This is what I have tried and the result is blank or nothing:
$( document ).ready(function() {
$('[name=Other6]').val($('[name=universal_leadid]').val());
//alert(universal_leadid);
//console.log($('[name=universal_leadid]'));
//console.log($('[name=universal_leadid]').val());
//console.log($('[name=Other6]').val());
$('#salutation').attr('checked',false);
//var myValue = $( "#leadid_token" ).val();
//document.getElementById('leadid_token');
//var x = document.getElementById("leadid_token").value;
//alert(x); // Showing Empty alert box
});
The result I want, is that every value populated on universal_leadid need to go to Other6 field
Your both the input fields are hidden
Please use
<input type="text" name="Other6" value="">
<input type="text" name="universal_leadid" value="KFLS00-D254-DD521-GF52-GHT554125" id="leadid_token">
Working Fiddle
Try this :
var token = $("#leadid_token").val();
$("input[name=Other6]").val(token);
console.log($("input[name=Other6]").val());
http://jsfiddle.net/8rw9cep6/

How to show Javascript variable inside input box ,so that i cant post it in html form? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
http://jsfiddle.net/t32h0gj4/4/
how to show variable inside input box<b id="smsCount"></b> SMS (<b id="smsLength"></b>) Characters left
<br />
<input type= "text" id= "smsCount" ></input>
<textarea id="smsText" style="width:400px;height:200px"></textarea>
Please help me
Fixed: http://jsfiddle.net/t32h0gj4/5/
Changes:
HTML: change id="smsCount" to class="smsCount" (id's need to be unique)
JavaScript: change the selector to .smsCount and further below also call the jQuery .val() function to set the input box.

Php variable in script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code:
<script>
newTextBoxDiv.after().html(<?php echo $do_shortcode('[dropdown]') ?>'<span class="wpcf7-form- control-wrap text-73'+ counter + '"> '+ '<input type="text" name="text-73'+ counter + '" value="" size="40" class="wpcf7-form-control wpcf7-text" aria-invalid="false"></span>'+'pocet<br>');
newTextBoxDiv.appendTo("#test");
</script>
After .html( I need get value from php. Could you tell me how to do it?
You have to generate proper Javascript. You're not. What you're doing is the equivalent of:
... .html(some text from php'<span....);
^---no opening quote
^---no closing quote
^--- no + to concatenate
Never EVER directly echo text from PHP to Javascript. Always use json_encode():
... .html(<?php echo json_encode($do_shortcode('[dropdown'])) ?> + '<span ...
Of course, this assumes that whatever function that $do_shortcode is pointing at will return some plain text. Adjust as needed for whatever it DOES return.
1) Make sure that your file is being evaluated as PHP - that is, its a .php file
2) Test to see if echo "aaa" works instead of echo $do_shortcode to see if PHP evaluation is working, but what isn't working is the "do short code" part. That will help you troubleshoot.
3) Use View Source to inspect the output.
Pls dont do thinks link this.
if you already have your data when you running the php scripte, why do you need javascript for rendering?
when you have to do it with javascript or you dont have your data at the point of rendering: use ajax.
Its simply confusing to render javascript with php that generates html.

Categories

Resources