I am using setAttribue as below. Its working only for first time and after that the changing value is showing the alert but not setting with document.getElementById("to").setAttribute("value", selValue);
document.getElementById("listcontact").onchange = function () {
var selIndex = document.getElementById("listcontact").selectedIndex;
var selValue = document.getElementById("listcontact").options[selIndex].innerHTML;
var contactVal = selValue.split(';');
var phone = contactVal[2];
alert(phone);
document.getElementById("to").setAttribute("value", selValue);
selIndex = "";
selValue = "";
phone = "";
selValue = "";
};
Why is this not working as I expect and how can I fix it?
The value attribute sets the initial value, not the current value.
Assign something to the value property instead.
document.getElementById("to").value = selValue;
You can use like this:
document.getElementById("to").value = selValue;
Related
I'm doing practical Javascript course on watchandcode.com and my code is exactly the same as the instructor's, but the part where it clears the inputs does not work here.
changeTodo: function () {
var changeTodoPositionInput = document.getElementById('changeTodoPositionInput').valueAsNumber;
var changeTodoTextInput = document.getElementById('changeTodoTextInput').value;
todoList.changeTodo(changeTodoPositionInput, changeTodoTextInput);
// THIS PART DOESN'T WORK
changeTodoPositionInput.value = '';
changeTodoTextInput.value = '';
}
The function does its job, it's just those last two lines that don't work.
You're trying to clear a value and not input elements, you have to get the DOM elements first :
var changeTodoPositionInput = document.getElementById('changeTodoPositionInput');
var changeTodoTextInput = document.getElementById('changeTodoTextInput');
Then get the value after that from them :
todoList.changeTodo(changeTodoPositionInput.valueAsNumber, changeTodoTextInput.value);
And now you could clear the value of the both inputs using :
changeTodoPositionInput.value = '';
changeTodoTextInput.value = '';
FULL CODE :
changeTodo: function() {
var changeTodoPositionInput = document.getElementById('changeTodoPositionInput');
var changeTodoTextInput = document.getElementById('changeTodoTextInput');
todoList.changeTodo(changeTodoPositionInput.valueAsNumber, changeTodoTextInput.value);
changeTodoPositionInput.value = '';
changeTodoTextInput.value = '';
}
Hope this helps.
You can try this:
var changeTodoPositionInput = document.getElementById('changeTodoPositionInput').value = "";
var changeTodoTextInput = document.getElementById('changeTodoTextInput').value = "";
I have two functions in JavaScript. One is change_BASE_ID() and the other one is Display_img(a,b).
change_BASE_ID() is called on mouseclick and internally calls Display_img(). On mouseover, Display_img() is called.
So Display_img() is used in two ways. There is a base_id_mitali variable in change_BASE_ID() which I want to be global. So even on mouseover when the Display_img() function is called independently, it should make use of the value of that variable.
If the onclick function was never clicked the value in base_id_mitali should be 01 or else if it was clicked it should be the one previously set ones.
var base_id_mitali = "";
function change_BASE_ID(base_ID, cursor_ID) { // THIS IS WHEN MOUSE IS CLICKED
//var curr_base_id = 'ch01ch01.png';
var start_name = "ch";
base_id_mitali = "01";
var bsid = document.getElementById('image').src;
//var bsidlen=bsid.charAt(bsid.length-6);
var bid1 = bsid.charAt(bsid.length - 6);
var bid2 = bsid.charAt(bsid.length - 5);
document.getElementById("mitali").innerHTML = "trying to get base id ".concat(bid1).concat(bid2);
base_id_mitali = concat(bid1).concat(bid2);
var a = base_ID;
var b = cursor_ID;
var temp_res1 = start_name.concat(base_id_mitali);
var temp_res2 = temp_res1.concat("ch");
var temp_res3 = temp_res2.concat(b);
var final = temp_res3.concat(".png");
curr_base_id = final;
document.getElementById('image').src = final;
Display_img(base_ID, cursor_ID);
document.getElementById("demo").innerHTML = "clicked on ".concat(final);
//setbaseid(base_id_mitali);
}
function Display_img(a, b) {
var start_name = "ch";
//document.getElementById("globalvar").innerHTML = "trying see global variable value ".concat(base_id_mitali);
var temp_res1 = start_name.concat(a); //want to use the global variable instead of a
var temp_res2 = temp_res1.concat("ch");
var temp_res3 = temp_res2.concat(b);
var final = temp_res3.concat(".png");
document.getElementById("demo").innerHTML = final;
document.getElementById('image').src = final;
}
I cannot see any initialization for the variable base_id_mitali in the code. Outside of your html code, initialize the variable on load using following code :
<script>
var base_id_mitali= "";
</script.
Now you can access this global variable .
In Google App Scripts (GAS), I want to be able to add and remove TextBox and TextArea elements to a FlexTable (that's being used as a form) and not worry about how many there are. I've named the text elements based on a counter to make this process easier.
So, is there a way to get the number of inputs (TextBox + TextArea) passed to e.parameter after the form is submitted?
Here's the relevant code from the FlexTable:
function doGet() {
var app = UiApp.createApplication();
var flex = app.createFlexTable().setId('myFlex');
var counter = 0;
var row_counter = 0;
...
var firstnameLabel = app.createLabel('Your FIRST Name');
var firstnameTextBox = app.createTextBox().setWidth(sm_width).setName('input' + counter).setText(data[counter]);
flex.setWidget(row_counter, 1, firstnameLabel);
flex.setWidget(row_counter, 2, firstnameTextBox);
row_counter++;
counter++;
var lastnameLabel = app.createLabel('Your LAST Name');
var lastnameTextBox = app.createTextBox().setWidth(sm_width).setName('input' + counter).setText(data[counter]);
flex.setWidget(row_counter, 1, lastnameLabel);
flex.setWidget(row_counter, 2, lastnameTextBox);
row_counter++;
counter++;
...
var submitButton = app.createButton('Submit Proposal');
flex.setWidget(row_counter, 2, submitButton);
var handler = app.createServerClickHandler('saveProposal');
handler.addCallbackElement(flex);
submitButton.addClickHandler(handler);
var scroll = app.createScrollPanel().setSize('100%', '100%');
scroll.add(flex);
app.add(scroll);
return app;
}
And here's the code for the ClickHandler (notice that I currently have 39 elements in my FlexTable):
function saveProposal(e){
var app = UiApp.getActiveApplication();
var userData = [];
var counter = 39;
for(var i = 0; i < counter; i++) {
var input_name = 'input' + i;
userData[i] = e.parameter[input_name];
}
So, is there a way to get the number of elements (in this case 39) without manually counting them and assigning this value to a variable?
I'm new at this stuff and I'd appreciate your help.
Cheers!
The simplest way is to add a hidden widget in your doGet() function that will hold the counter value like this :
var hidden = app.createHidden('counterValue',counter);// don't forget to add this widget as a callBackElement to your handler variable (handler.addCallBackElement(hidden))
then in the handler function simply use
var counter = Number(e.parameter.counterValue);// because the returned value is actually a string, as almost any other widget...
If you want to see this value while debugging you can replace it momentarily with a textBox...
You can search for arguments array based object.
function foo(x) {
console.log(arguments.length); // This will print 7.
}
foo(1,2,3,4,5,6,7) // Sending 7 parameters to function.
You could use a while loop.
var i = 0;
var userData = [];
while (e.parameter['input' + i] != undefined) {
userData[i] = e.parameter['input' + i];
i++;
};
OR:
var i = 0;
var userData = [];
var input_name = 'input0';
while (e.parameter[input_name] != undefined) {
userData[i] = e.parameter[input_name];
i++;
input_name = 'input' + i;
};
Please check the script below.
Dynamic form, so the script also dynamic, I have to calculate when the form data changes. during this i am getting some problem.
am getting the value from the variable Final_price1, Final_price2 .....,Final_price7, Final_price8 and then am calculating the total of those.
During this calculation, am concatenating the following concat("Final_price",i); to get the values of the above. This concatenated correctly, but the above variables values are not coming. I dont know why the values are not getting there. So check the script and update me.
function assign_body()
{
var a_7= document.getElementById("option[280]").value;
var spl_7 = a_7.split("_");
//alert(spl);
var cr_7 = spl_7[1];
var operator3_7 = cr_7.split("[");
var symbol7 = operator3_7[0];
var dtt_7 = operator3_7[1];
var myarr_7 = dtt_7.split("$");
var symbol_st_7 = myarr_7[1];
//alert(symbol_st);
//alert(symbol_s);
//var symbol_a = symbol_s.split("(");
//var symbol = symbol_a[1];
//alert(symbol);
var split_value_7 = myarr_7[1];
//alert(split_value);
var final_value_7 =symbol_st_7.split(".");
var Final_price7 =final_value_7[0];
var a_8= document.getElementById("option[281]").value;
var spl_8 = a_8.split("_");
//alert(spl);
var cr_8 = spl_8[1];
var operator3_8 = cr_8.split("[");
var symbol8 = operator3_8[0];
var dtt_8 = operator3_8[1];
var myarr_8 = dtt_8.split("$");
var symbol_st_8 = myarr_8[1];
//alert(symbol_st);
//alert(symbol_s);
//var symbol_a = symbol_s.split("(");
//var symbol = symbol_a[1];
//alert(symbol);
var split_value_8 = myarr_8[1];
//alert(split_value);
var final_value_8 =symbol_st_8.split(".");
var Final_price8 =final_value_8[0];
var j=8;
var total_amount=0;
for(var i=1; i<=j; i++)
{
final_prices=concat("Final_price",i);
alert(final_prices);
symbol_prices=concat("symbol",i);
alert(symbol_prices);
if(isNumber(final_prices)){
alert("number");
/*if(symbol_prices =='+') {
alert("plus");
var total_amount+=parseInt(original_prices)+parseInt(final_prices);
calculated_price_element.innerHTML=total_amount;
alert(total_amount);
} else if(symbol_prices =='-') {
alert("minus");
var total_amount+=parseInt(original_prices)-parseInt(final_prices);
calculated_price_element.innerHTML=total_amount;
alert(total_amount);
}*/
//alert('test');
}
}
}
How can I get text value of a tag when tag is stored as string?
var value = "<div>teeext</div>";
tag can be anything, its attributes may contain ">" string as well, regexp can be dangerous - also text value itself can be another tag.
You can make a dummy element:
var value = "<div>teeext</div>";
var div = document.createElement('div');
div.innerHTML = value;
var text = div.innerText || div.textContent;
Using just javascript:
var div = document.createElement('div');
var value = "<div>teeext</div>";
div.innerHTML = value;
var element = div.firstChild
console.log(element.innerHTML); //this the stuff in the tag
Using jquery:
$(value).html();
I guess that you're trying to strip tags.. In that case you can do it like this:
originalString = "<div>teeext</div>";
var value = originalString.replace(/(<([^>]+)>)/ig,"");
Test it out. If there are any exceptions which you need to handle, then just comment here and I'll try to help you further.
Edit for multiple tags:
originalString = "<div>teeext</div>";
outputString = originalString;
while (outputString.indexOf(/(<([^>]+)>)/ig) !== -1){
outputString = outputString.replace(/(<([^>]+)>)/ig,"");
}
value = outputString;
Haven't tested it, but you get the point ;)
Does this help as a starter?
function getText(tagString) {
var firstOpenTag = tagString.indexOf(">"),
lastCloseTag = tagString.lastIndexOf("</"),
substrLength;
if (firstOpenTag == -1 || lastCloseTag == -1) {
return tagString;
}
substrLength = (tagString.length - firstOpenTag) - (tagString.length - lastCloseTag) - 1;
return tagString.substr(firstOpenTag + 1, substrLength);
}
var value = "<div>teeext</div>";
console.log(getText(value));
var moreTags = "<div><ul><li>Text</li></ul></div>",
returnValue = moreTags,
prevReturnValue = "";
while (returnValue !== prevReturnValue) {
prevReturnValue = returnValue;
returnValue = getText(returnValue);
}
console.log(returnValue);