I am trying to get the following working. It seemed to work initially, but somehow it stopped working
var setCommonAttr = "1_row1_common";
var val = document.getElementById("abc_" + eval("setCommonAttr")).value;
what is wrong with above?
The above code is little different from what I am trying to accomplish. I gave the above example just not to make things complicated here. Below is what I am trying to accomplish:
First I am getting an existing element as follows. The element is a
<tr id="row_1_4_2009_abc" class="rowclick">
<td></td>
</tr>
I am using jquery to get the id on click of a row:
$(".rowclick").click(function() {
var row_id = $(this).attr("id");
var getAttributes = row_id.split("_");
var setCommonAttr = getAttributes[1] + "_" + getAttributes[2] + "_" + getAttributes[3] + "_" + getAttributes[4];
var new_row_id = document.getElementById("new_row_" + setCommonAttr).value;
});
You shouldn't need eval() to do this. The value you want is already a variable in JavaScript. Try:
var setCommonAttr = "1_row1_common";
var val = document.getElementById("abc_" + setCommonAttr).value;
Will everything be in that form? row_xxx_xxx_xxx_xxx
if so, why not var new_row_id = document.getElementById("new_" + row_id).value;
You don't need to call eval().
You can just concatenate the string with the variable:
var setCommonAttr = "1_row1_common"; var val = document.getElementById("abc_" + setCommonAttr).value;
Related
Normally My code is working fine. but, I get Syntax error. what is mistake in syntax here?
$(document).ready(function() {
$('.myClass').change(function() {
var ids = ['first', 'second'];
var totalCount = ids.reduce((prev, id) => parseInt($(`#${id}-passenger`).val()) + prev, 0);
var mc = $('input[name="myClass"]:checked + label').text();
$('#myTotal').val(totalCount + ' - '+mc);
});
});
Maybe you have a Javascript version issue and you should try without templating ?
var totalCount = ids.reduce(function (prev, id) {
return parseInt($('#' + id + '-passenger').val()) + prev
}, 0);
Hope it helps.
Your code is entirely valid and works fine. Therefore I would surmise that this issue is simply that the JS linter in the IDE you're using is outdated and doesn't support ES6. I'd suggest using a more up to date linter, assuming the IDE lets you change it, or even a better IDE entirely.
If you want to avoid the issue you would need to remove the template literals and arrow functions, like this:
$('.myClass').change(function() {
var ids = ['first', 'second'];
var totalCount = ids.reduce(function(prev, id) {
return parseInt($('#' + id + '-passenger').val()) + prev, 10);
}, 0);
var mc = $('input[name="myClass"]:checked + label').text();
$('#myTotal').val(totalCount + ' - ' + mc);
});
How can i get the email address from this script
<script>
var wcRQlKjfP = 'sales#websaucestudio##.com';
var wcRQlKjfP = wcRQlKjfP.split('#');
document.getElementById('wcRQlKjfP').innerHTML = wcRQlKjfP[0]+wcRQlKjfP[2]+wcRQlKjfP[1]; document.getElementById('wcRQlKjfP').href = 'mailto:'+wcRQlKjfP[0]+wcRQlKjfP[2]+wcRQlKjfP[1];
</script>
Help me!
Thanks a lot!
Although your variable has a strange name, all you need to do is combine the three parts (wcRQlKjfP[0] + wcRQlKjfP[2]).
wcRQlKjfP[1] can be completely ignored, as it evaluates to an empty string.
Here I'm logging the result to the console, but you could always assign to a new variable and then reference that variable.
var wcRQlKjfP = 'sales#websaucestudio##.com';
var wcRQlKjfP = wcRQlKjfP.split('#');
document.getElementById('wcRQlKjfP').innerHTML = wcRQlKjfP[0] + wcRQlKjfP[2] + wcRQlKjfP[1];
document.getElementById('wcRQlKjfP').href = 'mailto:' + wcRQlKjfP[0] + wcRQlKjfP[2] + wcRQlKjfP[1];
console.log(wcRQlKjfP[0] + wcRQlKjfP[2]);
<a id="wcRQlKjfP"></a>
Hope this helps! :)
I have the following code which is really bloated
$(".field-name-field-parts-status .field-item:contains('Submitted'), .field-name-field-parts-status .field-item:contains('Saved'), .field-name-field-parts-status .field-item:contains('HMNZ Approved')").addClass('btn-primary');
I tried to neaten it up by adding a var
var fieldItemStatus = $(".field-name-field-parts-status .field-item");
So it looked like this
$(fieldItemStatus + ":contains('Submitted'), " + fieldItemStatus + ":contains('Saved'), " + fieldItemStatus + ":contains('HMNZ Approved')").addClass('btn-primary');
But it stopped working, can anyone tell me what I did wrong? Thanks
Because you are trying to add a jQuery object and a string together. It does not work like that.
var fieldItemStatus = $(".field-name-field-parts-status .field-item");
should be a string
var fieldItemStatus = ".field-name-field-parts-status .field-item";
other option is to use filter.
You need to use .filter()
fieldItemStatus.filter(":contains('Submitted'), :contains('Saved'), :contains('HMNZ Approved')").addClass('btn-primary');
fieldItemStatus is an object so
fieldItemStatus + ":contains('Submitted'), " + fieldItemStatus + ":contains('Saved'), " + fieldItemStatus + ":contains('HMNZ Approved') will create a string like [Object object]:contains('Submitted'), [Object object]:contains('Saved'), [Object object]:contains('HMNZ Approved')
remove $ in front for fieldItemStatus
var fieldItemStatus = ".field-name-field-parts-status .field-item";
Because you want to use a jQuery Object to concat string. The right way to do this is using string all the time.
var fieldItemStatus = ".field-name-field-parts-status .field-item";
$(fieldItemStatus + ":contains('Submitted'), " + fieldItemStatus + ":contains('Saved'), " + fieldItemStatus + ":contains('HMNZ Approved')").addClass('btn-primary');
You could use the filter method:
fieldItemStatus.filter(":contains('Submitted'), :contains('Saved'), :contains('HMNZ Approved')").addClass('btn-primary');
Another option is using the filter callback function:
var items = ['Submitted', 'Saved', 'HMNZ Approved'];
fieldItemStatus.filter(function(_, el) {
return items.some(function(item) {
return el.textContent.indexOf(item) > -1;
});
});
.
A more procedural approach. This way if you want to easily change the selectors, just change the contains array. You could turn this into a function to easily retrieve your selector on demand elsewhere in the script.
var contains = ['Submitted','Saved','HMNZ Approved'];
var selector = '';
for(var i = 0; i < contains.length; i++) {
selector += '.field-name-field-parts-status .field-item:contains("' + contains[i] + ')';
if(i < contains.length - 1) selector += ', ';
}
$(selector).addClass('btn-primary');
I have a JavaScript, which returns 2 variables. I just want to access those variables in the generic handler(ashx) page but I can't. Can anybody give some suggestion?
var myArray = [txt, value];
var url = "insertComments.ashx?dat=" + myArray.join();
Change your Javascript :
var url = "insertComments.ashx?datTxt=" + txt + "&" + "datValue=" + value;
and in handler access that values with :
string txt = context.Request.Params["datTxt"];
string val = context.Request.Params["datValue"];
When I click search button the url parameter is taking care of. I need to change that url parameter name and value using jquery.
per example: search button clicked
http://testsite/_layouts/OSSSearchResults.aspx?k=deana&cs=This%20Site
Replace with:
http://testsite/_layouts/OSSSearchResults.aspx?k=deana&s=All%20Sites
I can not modify search button functionality, because it is out of the box functionality.
I can do changing url parameter and value. How can we do that?
Changing the parameter of a URL should be fairly straightfoward:
var param = window.location.href;
var param_q = param.split('?');
var param_ampersand = param_q[1].split('&');
var param_eq_1 = param_ampersand[0].split('=');
var param_eq_2 = param_ampersand[1].split('=');
var new_param_name_1 = 'test';
var new_param_value_1 = 'example';
var new_param_name_2 = 'test2';
var new_param_value_2 = 'example2';
// avoid infinite loop
if ((param_eq_1[0] != new_param_name_1 || param_eq_1[1] != new_param_value_1) || (param_eq_2[0] != new_param_name_2 || param_eq_2[1] != new_param_value_2)) window.location = param_q[0] + '?' + new_param_name_1 + '=' + new_param_value_1 + '&' + new_param_name_2 + '=' + new_param_value_2;
This worked for me in Chrome/FF. This just redirects the parameter - not sure if this is what you're looking for.
EDIT:
Added a logic to handle two parameters.