Generating Radio Button Result Depending On TextArea Value - javascript

A few days ago I did not manage to generate a result from my radio button, I managed to solve that problem now. My other problem is how do I generate ONLY few set of the radio button depending on TextArea value changes;
For Example in this code
<script language="javascript" type="text/javascript">
function generatetest() {
codeOne = document.docContainer.text1.value;
codeTwo = document.docContainer.number2.value;
function firstType(){
var codeFour = 0;
for( i = 0; i < document.docContainer.radio4.length; i++ )
{
if( document.docContainer.radio4[i].checked == true )
codeFour = document.docContainer.radio4[i].value;
}
var codeTen = 0;
for( i = 0; i < document.docContainer.radio10.length; i++ )
{
if( document.docContainer.radio10[i].checked == true )
codeTen = document.docContainer.radio10[i].value;
}
document.docContainer.textarea12.value = codeOne + codeTwo + codeFour + codeTen;
}
function secondType(){
var codeSix = 0;
for( i = 0; i < document.docContainer.radio6.length; i++ )
{
if( document.docContainer.radio6[i].checked == true )
codeFour = document.docContainer.radio6[i].value;
}
var codeEleven = 0;
for( i = 0; i < document.docContainer.radio11.length; i++ )
{
if( document.docContainer.radio11[i].checked == true )
codeTen = document.docContainer.radio11[i].value;
}
document.docContainer.textarea12.value = codeOne + codeTwo + codeSix + codeEleven
}
if (document.docContainer.number2.value="1")
{
firstType();
}
else if (document.docContainer.number2.value="2")
{
secondType();
}
}
</script>
The code is working, problem is, even though I insert the value on textarea "number2" as "2", the code still generate firstType() result
Am Im using wrong if-statement or what and how do I go around about this?

Your code could be more fun to read, but Putting quotes around your numbers is causing javascript to treat them like text. Is this what you want to be doing?

Related

Illustrator scripting for text baseline (javascript)

I'm trying to make a script for illustrator but my code seems to not be working and doing nothing on me.
It's supposed to make the "(" and ")" characters baselines to be 3px.
Looking for someone who can make this work.
function test(){
var doc = app.activeDocument;
var t = doc.textFrames[0], thisChar, thisSize, thisBaseline;
for(var i=0; i<t.characters.length; i++){
thisChar = t.characters;
if(thisChar.contents == "(" || thisChar.contents == ")"){
thisSize = thisChar.characterAttributes.size;
thisBaseline = thisChar.characterAttributes.baselineShift;
thisChar.characterAttributes.baselineShift = 3;
}
};
};
test();
var frames = app.activeDocument.selection;
for (var j=0; j<frames.length; j++) {
if (frames[j].typename == "TextFrame") {
for (var i=0; i<frames[j].characters.length; i++) {
var ch = frames[j].characters[i];
if( ch.contents == "(" || ch.contents == ")" ) ch.baselineShift = 3;
}
}
}
It shifts baseline for all brackets within all selected objects.
Note: this simple implementation doesn't handle grouped objects.
var frames = app.activeDocument.textFrames;
for (var j=0; j<frames.length; j++) {
for (var i=0; i<frames[j].characters.length; i++) {
var ch = frames[j].characters[i];
if( ch.contents == "(" || ch.contents == ")" ) ch.baselineShift = 3;
}
}
This variant of the script does the work within all text frames (grouped or not). You don't have to select anything.

Different ways of counting characters in Indesign using javascript

I'm writing a javascript program to handle each character intŠ¾ Indesign document.
To begin with, I wrote two different ways of counting characters, which for some reason give different results for large documents. Why?
var
myDocument, docStories, docCharacters,
docFootnotesCharacters, docTablesCharacters;
myDocument = app.activeDocument;
var TotalChars = 0;
// Fisrt way
docStories = myDocument.stories.everyItem();
docCharacters = docStories.characters.length;
docFootnotesCharacters = docStories.footnotes.everyItem().characters.length;
docTablesCharacters = docStories.tables.everyItem().cells.everyItem().characters.length;
statReport = [];
// Second way
for ( j = 0; j < myDocument.stories.length; j++ ) {
myStory = myDocument.stories.item(j);
var Frames = myStory.textContainers;
for ( i = 0; i < Frames.length; i++ ) {
var Frame = Frames[i];
for (var TextCnt = 0; TextCnt < Frame.texts.length; TextCnt++) {
CurrentText = Frame.texts.item(TextCnt);
TotalChars += CurrentText.characters.length;
}
for (var TableCnt = 0; TableCnt < Frame.tables.length; TableCnt++) {
var CurrentTable = Frame.tables.item(0);
for ( var CellCnt = 0; CellCnt < CurrentTable.cells.length; CellCnt++ ) {
var CurrentCell = CurrentTable.cells.item(CellCnt);
TotalChars += CurrentCell.characters.length;
}
}
for (var FootNoteCnt = 0; FootNoteCnt < Frame.footnotes.length; FootNoteCnt++) {
var CurrentFootNote = Frame.footnotes.item(0);
TotalChars += CurrentFootNote.characters.length;
}
}
}
statReport.push ( "Characters: " + ( docCharacters + docFootnotesCharacters + docTablesCharacters ) );
statReport.push ( "TotalChars: " + TotalChars );
alert ( statReport.join ( "\r" ), "Document Text Statistic" );
In the second method, you're counting all the characters inside text frames in the story. But stories can be overset (the text overflows). The first method will count overset text (because you're counting the characters in a story, but the second method will ignore those, because it's only counting characters in story frames.

I want to remove redundant code and move in to a separate Jquery function

Here is the code I am trying to remove the redundant code and move the code to separate function.
//Adding Infotypes to filter and checks whether any infotype option is selected
if(this.$infoOptions.val() != null){
var infotypelength = this.$infoOptions.val().length;
var l=0,j;
//Condition to check empty input type
if( infotypelength > 0){
var infotypeList = this.$infoOptions.val();
for(j = 0; j < infotypelength; j++) {
//It checks whether already option is selected and prevents adding to filter if its duplicate.
if(($.inArray( $('#infoOptions').select2('data')[j].text, filterList)) == -1 ){
this.filter.push($('#infoOptions').select2('data')[j].text);
if(infotypeList[j].contains('_subgroup')){
var res = infotypeList[j].split("_");
this.aSubinfotype[l]=res[0];
l=l+1;
}
else
this.aInfotypes.push(infotypeList[j]);
}
}
}
}
//Adding Countries to filter
if(this.$countryOptions.val() != null){
var geoLength = this.$countryOptions.val().length;
//Condition to check empty input type
if( geoLength > 0){
var geoList = this.$countryOptions.val();
var l=0;
for(var j = 0; j < geoLength; j++) {
if(($.inArray( $('#countryOptions').select2('data')[j].text, filterList)) == -1 ){
this.filter.push($('#countryOptions').select2('data')[j].text);
if(geoList[j].contains('_subgroup')){
var res = geoList[j].split("_");
this.aSubgeotype[l]=res[0];
l=l+1;
}
else
this.aGeography.push(geoList[j]);
}
}
}
}
But I am facing problem in passing the variable and cached selectors in to other function. Can anyone help me with this?
I don't know how is done your implementation but I really think that you can improve it, by the way, you can reduce your code in two way bit different :
var myFunction = function(option, filter, array, selector, subType) {
if(option && option.val()){
var optList = option.val();
var optLength = optList.length;
//Condition to check empty input type
if( optLength > 0) {
var l = 0;
for(var j = 0; j < optLength; j++) {
if( ($.inArray( selector.select2('data')[j].text, filterList)) == -1 ){
filter.push(selector.select2('data')[j].text);
if(optList[j].contains('_subgroup')){
var res = optList[j].split("_");
subType[l]=res[0];
l=l+1;
} else {
array.push(optList[j]);
}
}
}
}
}
}
call : myFunction(this.$countryOptions, this.filter, this.aGeography, $('#countryOptions'), this.aSubgeotype)
// data = {option, filter, array, selector, subType}
var myFunction = function(data) {
if(data.option && data.option.val()){
var optList = data.option.val();
var optLength = optList.length;
//Condition to check empty input type
if( optLength > 0) {
var l = 0;
for(var j = 0; j < optLength; j++) {
if( ($.inArray( data.selector.select2('data')[j].text, filterList)) == -1 ){
data.filter.push(data.selector.select2('data')[j].text);
if(optList[j].contains('_subgroup')){
var res = optList[j].split("_");
data.subType[l]=res[0];
l=l+1;
} else {
data.array.push(optList[j]);
}
}
}
}
}
}
call :
myFunction({
option: this.$countryOptions,
filter: this.filter,
array: this.aGeography,
selector: $('#countryOptions'),
subType: this.aSubgeotype
});
or
var data = {
option: this.$countryOptions,
filter: this.filter,
array: this.aGeography,
selector: $('#countryOptions'),
subType: this.aSubgeotype
}
myFunction(data);
The first way is to pass your data one by one, the second you pass your data into an json object.

Javascript to show any/all query values in the URL

I am working on a document list with a filter menu to pair down the listing via query strings. I want to display all current query values in a designated div, which I have working. The problem that I have now is that I have each category individually coded in the JS, which causes an "undefined" value to display if there is no query for that category.
I want to display ONLY the values that are represented in the query. I have been trying to create an array, but my JS is spotty at best and this has been kludged-together from multiple sources.
Any suggestions?
Here is my current js:
function getQueryVariable(variable)
{
try{
q = location.search.substring(1);
v = q.split("&");
for( var i = 0; i < v.length; i++ ){
p = v[i].split("=");
if( p[0] == variable ){
if( p[1].indexOf('_') != -1 ){
n = [];
for( var j = 0; j < p[1].split('_').length; j++ ){
n.push(p[1].split('_')[j]);
}
str = "";
for( var k = 0; k < n.length; k++ ){
str += n[k] + ' ';
}
return str.trim();
}
else{
return p[1];
}
}
}
}
catch (e){
console.log(e);
}
}
if(document.location.search.length) {
// query string exists
$p( "div#filterContainer" ).html(
"<div class='litType'><ul class='ct-list'><li class='ct-list-elem'>"
+getQueryVariable('application')
+"</li>"+
"<li class='ct-list-elem'>"
+getQueryVariable('year')
+"</li>"+
"<li class='ct-list-elem'>"
+getQueryVariable('industry')+
"</li>"+
"<li class='ct-list-elem'>"
+getQueryVariable('literature_type')
+
"</li></ul></div>" );
} else {
// no query string exists
$p( "div#filterContainer" ).html(
"No filters are being applied." );
}

jQuery - Sort an array and add a class to values with the same attribute, and the top 10

Scenario:
I am matching cases up via using the distance between two points
(using lat and long).
Everything is done in a table.
I get a list of 'tds' with class 'city', and make an array.
Then I sort the array based on 'data-distance' attribute
I then highlight the top ten, and if two 'data-distance' has the same
value, highlight them both, and then highlight, lets say 11 instead
of 10, because two have the same 'data-distance'
This is a screenshot of what I currently have, and an overview:
At the moment, it currently highlights the top ten, and if two values are the same, highlight them the same color, but it does not hightlight another one after 10.
Current Code
var c_tds = $('td.city');
var c_arr = $.makeArray(c_tds);
c_arr.sort(function(a,b) {
var A = $(a).attr('data-distance');
var B = $(b).attr('data-distance');
return B - A;
});
for ( var i = 1; i <= 5; i++ ) {
var A1 = $(c_arr[c_arr.length-i]); var A2 = $(c_arr[c_arr.length-(i-1)]);
if(A1.attr('data-distance') == A2.attr('data-distance')){ console.log(A1); }
A1.addClass('success');
}
for ( var i = 6; i <= 10; i++ ) {
var B1 = $(c_arr[c_arr.length-i]); var B2 = $(c_arr[c_arr.length-(i-1)]);
if(B1.attr('data-distance') == B2.attr('data-distance')){ console.log(i); if(i == 6) { B1.addClass('success'); } else { B1.addClass('warning'); } } else {
B1.addClass('warning');
}
}
Question
How can I get it so that if two values are the same, then add an extra one to highlight (keeping the minimum highlight count of 10)
You can feed the magic number 10 to a mutable variable:
var maxHighlightsLength = 10;
for ( var i = 6; i <= maxHighlightsLength ; i++ ) {
var B1 = $(c_arr[c_arr.length-i]); var B2 = $(c_arr[c_arr.length-(i-1)]);
if(B1.attr('data-distance') == B2.attr('data-distance')){ maxHighlightsLength++; if(i == 6) { B1.addClass('success'); } else { B1.addClass('warning'); } } else {
B1.addClass('warning');
}
}
Note that this way you get a corner case where everything is highlighted. You might want to add special handling for it (You can do the same for both loops)
jsFiddle
JS
function highlightTopTen() {
var tds = $( "td.city" );
tds.sort( function( td1, td2 ) {
return parseInt( $( td2 ).attr( "data-distance" ) ) - parseInt( $( td1 ).attr( "data-distance" ) );
} );
var i = 0;
var j = 0;
for( var i = 0; i < tds.length; i++ ) {
if ( i > 0 && parseInt( $( tds[ i ] ).attr( "data-distance" ) ) != parseInt( $( tds[ i - 1 ] ).attr( "data-distance" ) ) ) { j++; }
if ( j >= 10 ) {
break;
} else {
$( tds[ i ] ).addClass( "highlited" );
}
}
}

Categories