var myValue = "sItem-" + val;
$('#something').attr('class','myValue');
The DOM show me myValue instead of the value that I set, which I expect to see sItem-1. I console.log the myValue, but it can shows sItem-1? hmm...
I also tried $('#something').attr('class', "sItem-" + val); I doesn't work
You are enclosing the variable into single quotes.
Try this
$('#something').attr('class',myValue);
instead of
$('#something').attr('class','myValue');
Or even better use .prop() instead of .attr()
$('#something').prop('class',myValue);
Or if you want to add class to an element then you can use .addClass()
$('#something').addClass(myValue);
Use addClass instead of attr: http://api.jquery.com/addClass/
And you uses myValue with single quotes, that's wrong:
$('#something').addClass(myValue);
try something like this
var myValue = "sItem-" + val;
$('#something').addClass('class',myValue);
Related
I mean I know something like :
$(".class1, #idAbc, div.blahblah .etc").change(function( ...
is ok
But what if I already have :
myVar1 = $(".class1");
myVar2 = $("#idAbc");
myVar3 = $("div.blahblah .etc");
and I also have to apply the same event function on them ?
Do I need to write an event for each of them ?
I think I'd rather put the variabkles in an array and then iterate to apply the event, but what's the simplest way ?
myVar1.add(myVar2).add(myVar3).on('change', ...)
http://api.jquery.com/add/
You can use .selector property of jQuery object
$(`${myVar1.selector},${myVar2.selector},${myVar3.selector}`)
.on("change", function() {})
I have input in grid, which each unique ID, output as following;
I want to know what is equivalent to $(this).attr("ID") in javaScript
function load_getUserListByGroupID()
{
var selectedGroupID = document.getElementById('input');
alert(selectedGroupID);
}
You can simply get it using .id attribute like this:
this.id;
Or you can also use .getAttribute() method:
this.getAttribute('id')
So your example should be, like this:
function load_getUserListByGroupID(element) {
alert(element.id);
}
aa
Pass this to onclick event handler. Then you can directly get its id.
Use
Script
function load_getUserListByGroupID(elem){
alert(elem.id);
}
Use a variable,
onClick="reply_click(this.id)"
function reply_click(clicked_id)
{
alert(clicked_id);
}
Add the OnClick function the element you'd like, this will also throw the ID value when processing to the function.
Hope this helps :)
I'm looking for an easy way to assign to a variable depending on the value of another variable.
device.slot2_clipList[clipNumber] = singleClipDetails;
what I'm trying to do is: replace the "2" with another variable, so that i can run the same operation while just changing the
var slotNumber, and write to the corresponding variable.
i tried
device.slot + device.slotNumber + _clipList[clipNumber]
but (obviously?), this doesn't work.
How can this be done? (Maybe I named the Question incorrectly, but that was the closest I could think of.)
Thanks
This is what bracket notation is for
var i = 2;
device['slot' + i + '_clipList'][clipNumber] = singleClipDetails;
device['slotNumber' + _clipList[clipNumber] ]
Explanation:
foo.bar in javascript is identical (even in performance) to foo['bar']. So any object property name can be built up from strings.
I would like to be able to do this:
var b = $(this).is(':checked')
$('.perspective-col'). (b) ? show() : hide()
instead of
var b = $(this).is(':checked')
if(b) {
$('.perspective-col').show()
} else {
$('.perspective-col').hide()
}
Am I wishing for too much? Or is there some wonderful javascript syntax I haven't found yet? Or am I right in thinking that no such thing exists in JQuery thus far? (I'm using JQuery 1.9.0)
You can use this :
var b = $(this).is(':checked')
$('.perspective-col')[b ? 'show' : 'hide']()
You can call jQuery function by passing a string into the bracket []. Just insert a condition inside to decide which string you pass!
In general,
<any expression>.property
is equivalent to:
<any expression>['property']
The difference is that you can replace the literal 'property' in the brackets with an expression that calculates the property name. jQuery methods are just properties whose values happen to be functions.
But I actually hate that practice. You can also use jQuery .toggle()
jQuery has a method that does what you want called toggle():
$('.perspective-col').toggle(b);
How to shift between variable names in jQuery and change them?
//predefined variables
var s1='';
var d2='';
//trying to change variables by .attr() parameter but no luck
$('body').on('click','span', function() {
var $(this).parent().attr('data-scan')=$(this).attr('id');
});
HTML
<div data-scan="s1"><span id="banana">Banana</span><span id="apple">Apple</span></div>
<div data-scan="d2"><span id="orange">Orange</span><span id="apple">Apple</span></div>
How can I change specific variables? I do't care about changing attr papameter, all I need is changing predefined global var parameters!
You are using wrong syntax of attr()
Syntax: attr( attributeName , value)
Change
var $(this).parent().attr('data-scan')=$(this).attr('id');
To
$(this).closest('div').attr('data-scan',$(this).attr('id'));
You code would be
Live Demo
$('body').on('click','span', function() {
debugger
$(this).closest('div').attr('data-scan',$(this).attr('id'));
s1=$(this).closest('div').attr('data-scan');
alert(s1);
});
Firstly, your html for the data-scan attributes is wrong, you have no closing quotes.
Secondly, you can the data() jquery function to access data attributes.
Thirdly, you cannot set values by using the = operator.
You want something like this:
$(this).parent().data('scan', $(this).attr('id'));
or, without the data() function:
$(this).parent().attr('data-scan', $(this).attr('id'));
Here is a working example
To get the value you can do one of the following:
var dataScan = $(this).parent().data('scan');
or
var dataScan = $(this).parent().attr('data-scan');
Your exact requirements for setting a variable based on the data-scan value
Based on your comments and code, I think it has not been clear what you were trying to do. I think I have worked it out though and you want to use the data-scan value to determine which global variable should be set...
//predefined variables
var s1='';
var d2='';
$('body').on('click','span', function() {
var variableType = $(this).parent().data('scan');
var valueToSet = $(this).attr('id');
if(variableType == "s1"){
s1 = valueToSet;
}
else if(variableType == "d2"){
d2 = valueToSet;
}
});
Here is an example of what I think you are trying to do.
However, if you have lots of variables then it is not ideal to use so many if/else statements. So you could use the javascript eval() function.
var variableType = $(this).parent().data('scan');
var valueToSet = $(this).attr('id');
eval("" + variableType + " = '" + valueToSet + "';");
See here for an example
But be careful the your eval code is subjected to user injected values (not that javascript is safe from users anyway)
You're pretty close!
// set
$('#item').attr('data-scan', 'set new value');
// get
var dataScan = $('#item').attr('data-scan');
console.log(dataScan); //=> set new value
Probably best if you use .prop() rather than .attr()
$(this).parent().prop('data-scan', $(this).attr('id') );
As the jQuery api documentation states
As of jQuery 1.6, the .attr() method returns undefined for attributes
that have not been set. In addition, .attr() should not be used on
plain objects, arrays, the window, or the document. To retrieve and
change DOM properties, use the .prop() method.