I want to make a click on element using java script. Actual code to be click is as following which is working fine if I am using as it is. Aliases.LateralData_Applications_Dashboard.LoginForm.btnOK.ClickButton();
I am taking values from table in to 2 different string as following. Aliases.LateralData_Applications_Dashboard.LoginForm.btnOK
and
ClickButton();
Now I am trying to make click using combination of these 2 strings which is not working.
var tblObjectrepo = new db_Acess_connect();
var tblTestSteps = new db_Acess_connect1();
var elementPath=tblObjectrepo.field1; // getting value as Aliases.LateralData_Applications_Dashboard.LoginForm.btnOK
var elementAction=tblTestSteps.field1; // getting value as ClickButton
elementPath.elementAction; // this part of code is not working
Log.Message("Final Click: "+elementPath+"."+elementAction+";"); value is displaying as Final Click: Aliases.LateralData_Applications_Dashboard.LoginForm.btnOK.ClickButton;
Try this code:
var expr = elementPath + "." + elementAction + "()";
eval(expr);
Related
I am trying to get a vlaue from a data layer variable and add it to the end of a hyperlink.
So if my hyperlink is https://www.somehyperlink.com and my variable is variable1 with value 23 i want the final hyperlink to be https://www.somehyperlink.com/23.
I've got this code, which is probably not the best way to do it, but I'm not sure how to replace the last part of it with the value from the variable:
(function () {
var links = document.querySelectorAll( 'a[href="https://www.somehyperlink.com/replace"]')
var searchString = "replace"
var replacementString = "value-from-variable"
links.forEach(function(link){
var original = link.getAttribute("href");
var replace = original.replace(searchString,replacementString)
link.setAttribute("href",replace)
})
})();
I would appreciate any help.
Thanks
The code works, try to apply it in a Custom HTML Tag instead Custom Javascript variable.
Step 1:
create a Data Layer Variable called i.e. 'DLV value to replace' and assign the name of the variable in the Data Layer Variable Name field.
Step 2:
apply the Data Layer Variable to your Custom HTML code.
See below:
<script>
var links = document.querySelectorAll( 'a[href="https://www.somehyperlink.com/replace"]')
var searchString = "replace"
var replacementString = {{DLV value to replace}}
links.forEach(function(link){
var original = link.getAttribute("href");
var replace = original.replace(searchString,replacementString)
link.setAttribute("href",replace)
})
</script>
So I'm making a JQuery calculator in codepen and I'm basically having the buttons write out the equation. So far so good. However I run into some problems once I try to get back the result of the calculations. How do I convert the calculations element in HTML into a variable string in JQuery and then return it back into the HTML as the solution?
Here is my codepen project link, as well as the problematic section of code: http://codepen.io/Starkiller12/pen/eJYGXY
$("#equal").click(function() {
var x = $("p");
$("p").empty();
$("p").html(x);
})
You can do like following:
$("#equal").click(function() {
var arr = $('p').text().split('+');
var first=parseInt(arr[0]);
var two=parseInt(arr[1]);
$("p").empty();
$("p").html(first + two);
})
Here it is done for addition(+).
You can do same for other.
Working Fiddle
your variable is storing the jquery object, not its contents
so var x = $("p"); should be var x = $("p").text(); or var x = $("p").html();
I know the question sounds strange, but it's really very simple. I have the following function which isn't working:
function start40Counter(counter40_set){console.log(counter40_set);
var gid = counter40_set[0];
var seat = counter40_set[1];
var suits = counter40_set[2];
var cont = "";
$.each(suits, function (num, suit) {
cont += "<a class='suitpick' onClick='pickSuit(counter40_set);'><img src='"+base+"images/someimg.png' title='Odaberi' /></a>";
});
$('#game40_picks').html(cont);
}
counter40_set is [10, 3, ["H", "S"]]. The part of the function that fails is the part this:
onClick='pickSuit(counter40_set);'
It says that counter40_set is not defined. I understand that. This wouldn't even work if counter40_set was a simple string instead of an array. If I try onClick='pickSuit("+counter40_set+");' I get a different error, saying H is not defined. I get this too, the array is rendered and JS doesn't know what H and S are.
I also tried passing the array elements (counter40_set[0] etc) individually but it still fails with the last element (["H", "S"]).
So, how do I pass this data to the onClick function in this case? There must be a more elegant way than concatenating the whole thing into a string and passing that to the function?
Btw, this is a simplified version. What I should really be passing in every iteration is [suit, counter40_set] so that each link chooses a different suit. I'm asking the simplified question because that will be enough to send me down the right path.
It cannot work,because the context is lost and thus "counter40_set" is not set.
To fix it simply use jquery for the onlick as well:
$('#game40_picks').empty(); // get rid of everything
$.each(suits, function (num, suit) {
var line = $("<a class='suitpick'><img src='"+base+"images/"+cardsuits[suit].img+"' title='Odaberi "+cardsuits[suit].name+"' /></a>");
line.click(function(ev){
ev.preventDefault(); // prevent default click handler on "a"
pickSuit(counter40_set);
});
$('#game40_picks').append(line);
});
this way the "counter40_set" is available for the click function.
You shouldn't use the onClick HTML attribute. Also, using DOM functions to build nodes saves the time it takes jQuery to parse strings, but basically the method below is to create the element and attach a click event listener and then append it to the specified element.
function start40Counter(event){console.log(event.data.counter40_set);
var counter40_set = event.data.counter40_set;
var gid = counter40_set[0];
var seat = counter40_set[1];
var suits = counter40_set[2];
var cont = "";
$.each(suits, function (num, suit) {
var link = document.createElement('a');
link.className = 'suitpick';
$(link).on('click', {counter40_set: counter40_set}, start40Counter);
var img = document.createElement('img');
img.src= base + "images/" + cardsuits[suit].img;
img.title = 'Odaberi ' + cardsuits[suit].name;
link.appendChild(img);
$('#game40_picks').append(link);
});
}
Not tested but it might work out of the box.
Im trying to add an array to a webpage. I have tried a few different pieces of code show below but none of them work. I would like the output to be similar to a list like:
text1
text2
text3
...
The code I have used so far is:
var i;
var test = new Array();
test[0] = "text1";
test[1] = "text2";
test[2] = "text3";
// first attempt
$('#here').html(test.join(' '));
// second attempt
$(document).ready(function() {
var testList="";
for (i=0;i<test.length; i++) {
testList+= test[i] + '<br />';
}
$('#here').html('testList');
songList="";
});
I am quite new to javaScript so I am not sure if I have just made a small mistake or if Im doing this in the wrong way. Also, above is a copy of all the code in my javaScript file and some places online are saying I need to import something? Im not sure!
Thanks
Try without quotes:
$('#here').html(testList);
-or-
$('#here').html(test.join('<br />'));
Another approach:
var html = ''; // string
$.each(test,function(i,val){ // loop through array
var newDiv = $('<div/>').html(val); // build a div around each value
html += $('<div>').append(newDiv.clone()).remove().html();
// get the html by
// 1. cloning the object
// 2. wrapping it
// 3. getting that html
// 4. then deleting the wrap
// courtesy of (http://jquery-howto.blogspot.com/2009/02/how-to-get-full-html-string-including.html)
});
$('#here').html(html);
There might be more code in the latter, but it'll be cleaner in the long run if you want to add IDs, classes, or other attributes. Just stick it in a function and amend the jQuery.
Try changing the line
$('#here').html('testList')
to
$('#here').html(testList)
What you have works if you remove the single quotes from testList. However, if you would like an actual unordered list you can do this. (here's a jsFiddle)
var test = new Array();
test[0] = "text1";
test[1] = "text2";
test[2] = "text3";
// first attempt
$('#here').html(test.join(' '));
// second attempt
$(document).ready(function() {
var testList=$("<ul></ul>");
for (var i=0;i<test.length; i++) {
$(testList).append($("<li></li>").text(test[i]));
}
$('#here').html(testList);
songList="";
});
This line:
$('#here').html('testList');
shouldn't have single quotes around testList - you want to use the content of the variable, not the string literal "testList".
Don't pass the variable as a string : $('#here').html('testList'); Pass it without quotes : $('#here').html(testList);
Here's the simplest version:
$(document).ready(function() {
var test = ["text1", "text2", "text3"];
$('#here').html(test.join("<br>"));
});
iv'e got 2 html pages
one performs a "get" method on the other
these pages are duplicates in that they own they both poses a form with the same control types
and control names
when i submit my form from the source page my url string consist of the values appended together after a '?' char
....?txtName=era&txtAge=28&gender=male&langHe=on&langEn=on&select=1
in the detestation page onload i call a function which splits the control names and their values and sets them
// this is called from <body onload="f();">
function f() {
var st = new String(location.search);
st = st.substring(1, st.length);
var input = st.split('&');
var value;
var ctrl;
var val;
var _control;
for (var i = 0; i < input.length; i++)
{
value = input[i].substring(0, input[i].length);
ctrl = value.substring(0, value.indexOf('='));
val = value.substring(value.indexOf('=') + 1, value.length);
_control = document.getElementsByName(ctrl);
_control.value = val;
}
}
i debugged this function and checked that every thing is put in to place as it should
the problem is that after the value are set to the controls
they do not appear on the page , as if they didn't get set at all
additionally in Google chrome i get a "Aw,Snap!" Error after these actions (Aw Snap happens only when i debug )
i'm new to java-script and i'm guessing there's a problem with the way i'm assigning these values , i tried also just the first control which is a text type input and it also does not get updated .
any idea's on why this doesn't work ?
thanks in advance
eran.
Try changing the last line to:
_control[0].value = val;
UPDATE:
It would be a lot easier if you used jQuery:
$('[name='+ctrl+']').val(val);