String append from <select> to form new variable - javascript

I have a combobox in HTML:
<select id="dimensionName"><option>/Example.html</option></select>.
In my JavaScript, if I have a var URL1 = "facebook.com", and I want to append the selected string from the above combobox to the end of URL1, and set the appended string as a new variable as NewDimensionName, how can I do that?
I tried doing the following but to no avail:
var NewDimensionName = URL1.concat(document.getElementById("dimensionName").selectedItem);

Since you want to get the text, you would have to do it like this:
var x = document.getElementById("dimensionName");
var NewDimensionName = URL1+x.options[x.selectedIndex].text;
This gets the options array with the selected index and returns the text, not the value.
Also note that concat() is used for arrays, not strings. Simply using + will suffice here.

Can you try this,
var dimensionName= document.getElementById("dimensionName");
var dimensionText= dimensionName.options[dimensionName.selectedIndex].text;
var NewDimensionName = URL1 + dimensionText;
console.log("NewDimensionName ::" + NewDimensionName );

x = document.getElementById("dimensionName").options[dimensionName.selectedIndex].innerHTML;
x = URL1 + x;
alert x;

Related

How to use concatenation with a variable used in a JavaScript JQuery function

I'm trying to use a variable that contains the name of a HTML element in a JQuery function for a radio button.
Normally I would do something like this:
var oldRadioValue = $('input[name="correct10")"]:checked').val();
But due to the radio button being created dynamically I need to do something like this:
var radioName = "correct"+questionCount;
var oldRadioValue = $('input[name=radioName]:checked').val();
When I check the console.log() I am getting undefined...
Thanks in advance for any help!
You should be able to it like this do this
var oldRadioValue = $('input[name="' + radioName + '"]:checked').val();
You can either use concatenation like this:
var oldRadioValue = $('input[name="'+radioName+'"]:checked').val()
Or the newer template literal syntax:
var oldRadioValue = $(`input[name="${radioName}"]:checked`).val();
Your need string concatenation to make this work.
var oldRadioValue = $('input[name=' + radioName + ']:checked').val();

Replace different Values in String?

I have a string which looks like this: (id:561644cdb40fbe0100247dd7:q) (id:56165d8a79c8c40100adbdb6:q) and I need to replace the different id's with different values. I already have the id's in a variable and trying to loop through with something like this var mailId = "(id:" + rplcId + ":q)"; But If I use the replace() function it doesnt work...Any other suggestions?
You can select the id with:
"(id:56165d8a79c8c40100adbdb6:q)".split(":")[1]
var id = "(id:561644cdb40fbe0100247dd7:q)";
var idArr = id.split(":");
idArr[1] = newId; //56165d8a79c8c40100adbdb6
var mailId = idArr[0]+idArr[1]+idArr[2];
and please provide your full code

jquery, append multiple values to one input hidden

I have a following variable
var $pk3s_c = $('<input id = query_form_tbl_info_'+query_index +'_pk3ss[] name =query_form[tbl_info]['+query_index+'][pk3ss][] type = hidden></input>');
and an array
var pk3s = opts[tbl]["cols"];
during iteration through the array, I want to append the elements of an array to $pk3s_c
$.each(pk3s, function(i,pk3){
$pk3s_c.attr('value',pk3);
})
the code above is not working, it shows me that I have appended only last element of a pk3s, and not all of them. How can I append each element of p3ks into my hidden input?
You aren't going to get an array into a string field without converting it into a string.
The JSON format is very useful for this
// convert the array to a string
var myString = JSON.stringify(myArray);
// put the string into the field as it's value
$('input').val(myString);
Javascript can interpret the resulting string and server side languages can easily convert it from a string into an array value they can understand (see for php, ruby)
Here is an example in a jsfiddle
For readability, I would pass the arguments as a parameter to the jQuery function
Would look like
var values = ['argument1', 'argument2', 'argument3'];
var query_index = 43;
var jqueryAttributes = {
id: 'query_form_tbl_info_' + query_index + '_pk3ss[]',
name: 'query_form[tbl_info][' + query_index + '][pk3ss][]',
type: 'hidden',
value: JSON.stringify(values)
// if your server don't support HTML encoding, use the one below instead
// value: "['"+values.join("','")+"']"
};
var $newElement = $('<input/>', jqueryAttributes);
alert('you element would look like : ' + $newElement[0].outerHTML);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

Can't capitalize first letter, element not updated

I am getting my string from a textBox like this (in a loop where index is incremented):
var name = document.getElementById("TextBox" + index).value;
And I have a function in my script that looks like this meant to uppercase first letter:
function capital(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
And try to use it like this:
var name = document.getElementById("TextBox" + index).value;
capital(name);
They are in different functions in the same script tag. Where am I going wrong?
Make sure you assign the modified value back to the control:
var element = document.getElementById("TextBox" + index);
var name = element.value;
element.value = capital(name);
Are you trying to getElementById using the input type, or do you have actual IDs on your textbox called 'TextBox'? The below works fine, see fiddle
var index = 1;
var name = document.getElementById("test" + index).value;
function capital(str)
{
return str.charAt(0).toUpperCase() + str.slice(1);
}
alert(capital(name));
String are immutable in js, you need to reassign.
var name = capital(document.getElementById("TextBox" + index).value);
Plus strings are passed by value, so they are copied over to the new function. So whatever you do in the function won't cause any effects in your name variable.

Use javascript variable as array index

I have following code:
var n = 1;
var term = "${abc[n].term}";
console.log("term = " + term);
term seems to be empty, but if I replace
var term = "${abc[n].term}";
by
var term = "${abc[1].term}";
it works.
It looks like jsp is looking for the n property of the deck object, how could I fix it so that n is replaced by its value when I use is as array index ?
Edit:
It seems that it's not a good idea to try mixing JSTL and Javascript, and that if you want to use a javascript variable as array index, you must copy the object to an Array object, like this:
var deck = new Array();
<c:forEach var="v" items="${abc}">
deck.push("${v.term}");
</c:forEach>
var n = 1;
console.log("term = " + deck[n]);
You are not using quotes properly, try this:
var term = "${abc[" + n +"].term}";
"${abc[n].term}" here n is considred as a part of the string not as your variable n. So try concatenating it.

Categories

Resources