Remove first character with jQuery - javascript

How would I go about removing the first character from this.className in the below line?
The first variable will be _ and then a number. I just want the number to be assigned to className.
className = this.className;
Furthermore I am changing "$('.inter').html(window[link[className]]);" to use an array instead of the className variable. Is the below code the correct way to use an array with the index as a variable?
$('.inter').html(window[link[className]]);

No need to use jQuery for that, just plain ol' javascript using .substring
var trimmed = this.className.substring(1);

Related

.length property change inside a .substr()?

Here is some I just noticed and im asking if its normal. Im using a .length property inside a .substr(), but it seems like the value of .length change during the .substr(). Here is a example here : https://jsfiddle.net/L11yg3y0/1/
var immastring = "Metaphysics"
var test = immastring.substr(2,immastring.length-2);
alert(test);
Shouldn't it output "taphysi" instead of "taphysics"? Because right now, it means that in the method .substr, they first remove the first two character, actualize the .length value and then remove the last two character.
I was just wondering because I already used this kind of method in other language like c++ and c#, but it wasn't working that way.
.substr takes the start index and the length of the substring. "Metaphysics" has length 11, so immastring.length - 2 is 9. "taphysics".length is indeed 9.
If you want to specify the end index, use .substring instead.
JavaScript has two substring methods, you picked the wrong one.
str.substr(start[, length])
vs
str.substring(indexStart[, indexEnd])
References:
MDN substr
MDN substring

Why does quoting matter with getElementById?

I have a dom object like this within an html page:
<textarea id="owctl7fzk">​foo</textarea>
When I try to access it with getElementById without quoting the id, it returns null.
window.document.getElementById(owctl7fzk)
but when I put the id within single or double quotes, it selects the object:
window.document.getElementById('owctl7fzk')
window.document.getElementById("owctl7fzk")
Why does the quotation matter in this case? In general, when you can you omit and when can you not?
Edit In the code I presented above, the id is a hexatridecimal number. I previously had a decimal number without quotes as id, and it had no problem. Why was that?
getElementById(owctl7fzk) selects the element whose id attribute is equal to the value of the owctl7fzk variable:
var owctl7fzk = "the_id";
var element = document.getElementById(owctl7fzk);
owctl7fzk is definitely not the same as "owctl7fzk", because the former is a variable name, while the latter is a string.
This is how JavaScript works. Just because you can omit the quotes in HTML doesn't mean that you can do the same in JavaScript.
It's because when the ID is within a quote, its a string that is used to identify the ID attribute, otherwise it's reference as a variable. If you are performing window.document.getElementById(owctl7fzk), javascript is searching for a variable named owctl7fzk. If you used var owctl7fzk = 'owctl7fzk', it shall work
It should be in quotes because you are passing a string parameter to the getElementById method. Strings need to be in quotes.
In javascript, as in most languages, the parser understands a quoted value to be a "string" and an unquoted one to be an identifier (this is oversimplifying, and in reality, it's the lexer, not the parser).
Specific to your question, an element's id is a string, and so must be represented as a string in your source. You can use a variable (represented in code by an identifier of your choice) to hold this string or to hold the element itself:
var my_id = "owctl7fzk";
var elem = document.getElementById(my_id);
alert( my_id + " is the same as " + elem.id );

strip a div and all child elements from a string with JavaScript replace RegEx

I have a block of HTML stored in a variable called address_form, within that block of HTML I want to remove, or replace, a portion of it. The part I want to replace is a div with an ID of address_container.
There's clearly something wrong with my RegEx here that i'm using with the replace function as it is not working:
var tempStr = address_form.replace('/\<div id=\"#address_container\"\>.*<\/div\>/', '');
I simply want to replace a string, within a string.
Since you've tagged your question with jQuery, then I would suggest you use jQuery to do this task. Something like:
var tempStr = jQuery(address_from).remove('#address_container').html();
Don't do that, just get the contents of the div and replace the parent of that div with the contents.
So
var tempStr = $('#address_container').html(); // or text()
$('#parent_of_address_container').html(tempStr);
Your regex is wrong. Use this instead:
address_form.replace(/<div id=["-]#address_container["-]>.*<\/div>/,'');
From #RidgeRunner
Correctly matching a DIV element, (which itself may contain other DIV
elements), using a single JavaScript regex is impossible. This is
because the js regex engine does not support matching nested
structures.

Using a variable when traversing elements in JQuery

Very quick and hopefully simple question.
I am trying to select a hidden input by value with some predefined variable.
var id = $("#puid").val();
$('input[value=id]').closest('tr').css({'background-color':'red'});
I thought the above code would have worked, however its not processing id as a variable. What is the right notation to do this? (I have tested the code by replacing id with the actual number and the rest of the code works fine).
remove it from the quotes, so the variable is concatenated into the string. They way you have it, it's looking for the literal value "id", and has no way of knowing that you're talking about a variable.
$('input[value='+id+']')
edit: more info - you could put double quotes around the id part, inside the strings, as in Nick's answer, which would make it safe to use with non-numeric ids. I omitted them since your example doesn't need them, as you said your ids are numeric.
Concatenate the string selector with the variable, like this:
var id = $("#puid").val();
$('input[value="' + id + '"]').closest('tr').css({'background-color':'red'});
Currently, it's looking exactly for this: value="id", but you want your variable there.

Javascript confusion

var allRapidSpells = $$('input[value^=RSW]');
Can anyone tell me what that does?
I would venture to guess that you're using MooTools, a JavaScript framework. The $$() function is used to select an element (or multiple elements) in the DOM.
More specifically, the $$('input[value^=RSW]'); syntax is selecting all input elements whose value attribute starts with RSW.
Other attribute selectors include:
= : is equal to
*= : contains
^= : starts-with
$= : ends-with
!= : is not equal to
~= : contained in a space separated list
|= : contained in a '-' separated list
Edit: It looks as though Prototype, another JavaScript framework, uses the same syntax.
Return all inputs that hava value starting with RSW
It calls the function named '$$' with the parameter 'input[value...' and assigns the returnvalue of that function to the var allRapidSpells.
Javascript doesn't consider the '$' to be a reserved character, which jQuery makes excellent use of.
It looks like it uses some CSS selectors using some javascript library, the CSS selectors return all input tags where the value begins RSW.
calls a method on the windows object called $$ and passes a string argument to it, which appears to be an xpath expression.
which returns input tags that contain an attribute called value starting with RSW.

Categories

Resources