Serlialize array - javascript

I'm trying to serialise my array but I'm getting nothing in the log.
I want all checkboxes called color[] but not the first one.
I've tried:
$("input[name='color[]:not(:first)']").serialize();
But this logs as blank.
The following works but includes the first one, so it must be a problem with the not part.
$("input[name='color[]']").serialize();

Your attribute equal selector is wrong $("input[name='color[]:not(:first)']")
$("input[name='color[]']:not(:first)").serialize();

The selector you are using is wrong.
Finish ] after the name=value selector.
$("input[name='color[]']:not(:first)").serialize();
Here are the mistakes highlighted:
$("input[name='color[]:not(:first)']").serialize();
// ^ finish selector here

Please use this
$("input[name='color[]']:not(:first)").serialize();

Related

object HTMLcollection[0] keeps returning undefined

Suppose we have something like:
1
2
3
When I try something like
alert(document.getElementsByClassName("my-list"))
I get object HTMLCollection. And if I try something like alert(document.getElementsByClassName("my-list")[0]) I get undefined. How can I get the first href in the list? So it would be "1" in this case.
Check this in Fiddler. Place the document.getElementsByClassName("my-list") in a round bracket and the add the index [0] to it.
**UPDATE**: Use `window.onload` to perform operations after all DOM elements
are loaded.
window.onload = function()
{
alert((document.getElementsByClassName("my-list"))[0])
}
1
2
3
It could happen if you add the script in the HTML header. In this case, you saw HTMLCollection; however, the items would be empty.
move the script to the bottom of the HTML body.
Also, just be sure to check your code, I had this problem from using querySelector. If you use querySelector it only returns one element (which will return undefined when iterating), while querySelectorAll creates a html collection which you can iterate through.

jQuery regex match help (optional condition)

I've got something like that:
[#parameter='value']
and
[#parameter]
and I need regexp to parse both cases,
in first returns parameter & value in the second only parameter
Now, I've got something like that
"[#short-name='shorty']".match(/\[\#(.*)\=\'(.*)\'\]/)
which returns
["[#short-name='shorty']", "short-name", "shorty"]
its great but only in first case, please help
EDIT:
I just figured that probably two matches could be replaced by one so if it's possible, a little extension of current solution will be great.
match something like:
PARENT//CHILD[#parameter='value']
or
PARENT//CHILD[#parameter]
or
PARENT//CHILD
should returns
[ parent, child, parameter, value ]
[ parent, child, parameter ]
[ parent, child ]
Thanks a lot!
EDIT 2:
Allright it's easy
/([A-Z]+)\/\/([A-Z]+)(\[#([^=\]]*)(?:='(.*)'])?)?/
if it's possible to do it easier, please show me how
This will work for your general case:
/\[#([^=\]]*)(?:='(.*)'])?/
I removed a lot of the unnecessary escaping you were doing. The value is in parentheses (not captured because of ?:) and then made optional. The \[#.* has to be changed to \[#[^=\]] so that it will only try to capture up to the equals or the close brace (either of which may be there).
You can also update this to correctly match quotes, or even omit them
/\[#([^=\]]*)(?:=(['"]?)(.*)\2])?/
EDIT: You can also match multiple values per line using a similar expression; just add .*? for the capture of the value.
"[#short-name='shorty'] [#long-name=\"longy\"] [#longest]".replace(
/\[#([^=\]]*)(?:=(['"]?)(.*?)\2])?/g,
function () { console.log(arguments[1], arguments[3]); })
This will yield "short-name, shorty", "long-name longy", "longest undefined"
Let the second part be optional.
Put some spaces to best view.
"[#short-name]".match(/\[\#(.*) (\=\'(.*)\')? \]/)
Wrapping the code with parentheses and adding a ? will do the trick.
http://metahtml.sourceforge.net/documentation/regex/regex_3.mhtml#SEC14

match input elements with keyup

I am not sure what's going on here.. I am trying to just match the val on an input from a variable, and every time I try, I get an object, object.
Here is the fiddle:
http://jsfiddle.net/GLnQx/1/
It's this piece of script that is screwing me up:
$("input.numberOfAccounts").keyup(function () {
$("input.pricingPerAccount").val($(newPricePerAccount));
});
newPricePerAccount will show me the correct number on alert, but when I try to put it in a val, it's no good.
What am I missing here?
$() is the jQuery selector method. newPricePerAccount is 5050, so it is trying to select a 5050 element (i.e. nothing), and it returns an object. I think you just want
.val(newPricePerAccount)
It should be
$("input.pricingPerAccount").val(newPricePerAccount);
Looking at your jsFiddle, newPricePerAccount is a JS variable, correct? In that case, you don't need the $() selector on it.
$ is only used to select DOM elements, not variables:
$('input.pricingPerAccount').val(newPricePerAccount);

bookmarklet: inserting text into textarea with js?

What I am doing wrong?
javascript:document.getElementsByTagName('textarea').innerHTML='inserted';
I want to create a bookmarklet to insert simple text to a textarea on a given webpage.
Use the value property rather than innerHTML and make sure your code evaluates to undefined, which you can do by wrapping it in a function with no return statement. If you don't do this, the contents of the page will be replaced with whatever your code evaluates to (in this case, the string 'inserted').
javascript:(function() {document.getElementsByTagName('textarea')[0].value = 'inserted';})();
Update 14 January 2012
I failed to spot the fact that the original code was treating document.getElementsByTagName('textarea') as a single element rather than the NodeList it is, so I've updated my code with [0]. #streetpc's answer explains this in more detail.
Unlinke getElementById, getElementsByTagName has an sat Elements because it returns an array array-like NodeList of the matching elements. So you'll have to access one of the elements first, let's say the first one for simplicity:
javascript:void((function(){document.getElementsByTagName('textarea')[0].value='inserted'})())
Also, as mentioned by others, value property rather than innerHTML here.
In case anyone wonders how to use the currently focused text field, use the following:
document.activeElement.value = "...";
In jQuery you have to use if this way:
for single element -> $('#element_id').html('your html here')
for all text areas -> $('textarea').val('your html here')
I have to confess that I`m not sure why it works this way but it works. And use rameworks, they will save you time and nerves.

JavaScript if statement returns wrong values

Does anybody knows why this snippet returns false even if the passed string is "Active"?
if ($('status_'+id).getText()=="Active")
I've also tried changing the code to
if ($('status_'+id).getText()==String("Active"))
and
if (String($('status_'+id).getText())=="Active")
and still no luck.
I've also checked $('status_'+id).getText() through console.log to verify if it really returns "Active"
i wonder why it doesnt work?
any ideas?
Silly question: are you sure the returned string doesn't contain spaces?
The first step in any debugging task is to check your assumptions. Use a debugger or a series of alerts to check the following:
what's the value of id?
does$('status_'+id) evaluate to a DOM
element?
what does
$('status_'+id).getText() actually
return

Categories

Resources