Why do the outputs only display in one of element
(#skip-result or #distinct-result) not both of them ?
but if i try outputting the result from those two observable
below to console it works
you can check code here
var input$ = Rx.Observable.fromEvent(input,'input')
var x$ = input$
.debounce(1000)
// skip two character in string, from input value
// then, display it in #skip-result
x$
.map(v=> v.target.value)
.filter(v=> v.length > 2)
.flatMap(v=>
Rx
.Observable
.from(v)
.skip(2)
.reduce((x,y)=> x + y)
)
.subscribe(s=>{
$('#skip-result').text('Skip Value is ' + s)
})
// search distinct in string, from input value
// then, display it in #distinct-result
x$
.map(e=> e.target.value)
.filter(e=> e.length > 0)
.flatMap(v=>
Rx
.Observable
.from(v)
.distinct()
.reduce((x,y)=> x + y)
)
.subscribe(x=>{
$('#distinct-result').text('Distinct value is ' +x)
})
In the JSBin you've referenced, you haven't imported jQuery, but you appear to be using the jQuery selector to set the contents of both the #skip-result and #distinct-result DOM elements. This will throw an error.
If instead you changed them to set the innerHTML property, which one subscription already looks to be doing, you should get your expected behaviour.
.subscribe(s => {
skipResult.innerHTML = 'Skip Value is ' + s
})
EDIT
After a second look, your markup isn't being closed properly. Specifically, #skip-result is being made a child of #distinct-result.
I'm implementing Google's Instant Search in my application. I'd like to fire off HTTP requests as the user types in the text input. The only problem I'm having is that when the user gets to a space in between first and last names, the space is not encoded as a +, thus breaking the search. How can I either replace the space with a +, or just safely URL Encode the string?
$("#search").keypress(function(){
var query = "{% url accounts.views.instasearch %}?q=" + $('#tags').val();
var options = {};
$("#results").html(ajax_load).load(query);
});
Try encodeURIComponent.
Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).
Example:
var encoded = encodeURIComponent(str);
encodeURIComponent works fine for me. we can give the url like this in ajax call.The code shown below:
$.ajax({
cache: false,
type: "POST",
url: "http://atandra.mivamerchantdev.com//mm5/json.mvc?Store_Code=ATA&Function=Module&Module_Code=thub_connector&Module_Function=THUB_Request",
data: "strChannelName=" + $('#txtupdstorename').val() + "&ServiceUrl=" + encodeURIComponent($('#txtupdserviceurl').val()),
dataType: "HTML",
success: function (data) {
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
Better way:
encodeURIComponent escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )
To avoid unexpected requests to the server, you should call encodeURIComponent on any user-entered parameters that will be passed as part of a URI. For example, a user could type "Thyme &time=again" for a variable comment. Not using encodeURIComponent on this variable will give comment=Thyme%20&time=again. Note that the ampersand and the equal sign mark a new key and value pair. So instead of having a POST comment key equal to "Thyme &time=again", you have two POST keys, one equal to "Thyme " and another (time) equal to again.
For application/x-www-form-urlencoded (POST), per http://www.w3.org/TR/html401/interac...m-content-type, spaces are to be replaced by '+', so one may wish to follow a encodeURIComponent replacement with an additional replacement of "%20" with "+".
If one wishes to be more stringent in adhering to RFC 3986 (which reserves !, ', (, ), and *), even though these characters have no formalized URI delimiting uses, the following can be safely used:
function fixedEncodeURIComponent (str) {
return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
}
I'm using MVC3/EntityFramework as back-end, the front-end consumes all of my project controllers via jquery, posting directly (using $.post) doesnt requires the data encription, when you pass params directly other than URL hardcoded.
I already tested several chars i even sent an URL(this one http://www.ihackforfun.eu/index.php?title=update-on-url-crazy&more=1&c=1&tb=1&pb=1) as a parameter and had no issue at all even though encodeURIComponent works great when you pass all data in within the URL (hardcoded)
Hardcoded URL i.e.>
var encodedName = encodeURIComponent(name);
var url = "ControllerName/ActionName/" + encodedName + "/" + keyword + "/" + description + "/" + linkUrl + "/" + includeMetrics + "/" + typeTask + "/" + project + "/" + userCreated + "/" + userModified + "/" + status + "/" + parent;; // + name + "/" + keyword + "/" + description + "/" + linkUrl + "/" + includeMetrics + "/" + typeTask + "/" + project + "/" + userCreated + "/" + userModified + "/" + status + "/" + parent;
Otherwise dont use encodeURIComponent and instead try passing params in within the ajax post method
var url = "ControllerName/ActionName/";
$.post(url,
{ name: nameVal, fkKeyword: keyword, description: descriptionVal, linkUrl: linkUrlVal, includeMetrics: includeMetricsVal, FKTypeTask: typeTask, FKProject: project, FKUserCreated: userCreated, FKUserModified: userModified, FKStatus: status, FKParent: parent },
function (data) {.......});
use jQuery.param().....
Description: Create a serialized representation of an array, a plain object, or a jQuery object suitable for use in a URL query string or Ajax request. In case a jQuery object is passed, it should contain input elements with name/value properties.
try this one
var query = "{% url accounts.views.instasearch %}?q=" + $('#tags').val().replace(/ /g, '+');
I was writing a function to remove given elements from an array and then i came to this particular situation:
function removeStudent(elm, list){
var elmToDelete = list.indexOf(elm);
console.log(elm + " element");
the console shows -1, so the given element couldn't be found in the list. However, if a search it as a concatenated stirng, it will return the index of given element:
function removeStudent(elm, list){
var elmToDelete = list.indexOf(elm+"");
console.log(elm + " element");
For the record, I'm currently using Chrome version 57.0.2987.133 (64-bit) in Mac OS.
these are the two functions (externally loaded):
function replaceText(element, text) {
if (element != null) {
clearText(element);
var newNode = document.createTextNode(text);
element.appendChild(newNode);
}
}
function replaceImage(element, maker, imageState) {
replaceText(element, "replacing image " + maker + " with " + imageState + " version");
var imagePath = "_img/coffeeMaker_";
if (maker != null)
{
document.getElementById("coffeeMakerImg"+ maker).src = imagePath + imageState + ".png";
}
}
now here's the part that calls these functions. *notice that the replaceText() is called from within replaceImage()
replaceText(cmStatus_01, "BREWING " + name + "'s " + size + " " + beverage);
replaceImage("feedback", "01", "full");
document.forms[0].reset();
okay. now here's the kicker: the FIRST replaceText() works fine in ALL browsers. the replaceImage() fails ONLY in Firefox which CONTAINS A CALL TO replaceText() that only JUST worked as advertised!! i could see how i might have screwed up the image replacement (even though i copy/pasted it from another working project that DOES replace the image in FF...so weird...), but i do NOT see how the replaceText() can fail: it just worked!
so: whaaaaat!? i'm thinking its some kind of scope issue, but i'm stumped as to why.
totally stumped. forehead really sore...
thank for your time and help. i'm praying this isn't something really retarded...
WR!
PS: i'm also confused why, if i remove the quotes from the element name in the replaceImage() call, it breaks; but it works in the replaceText() call without brackets just fine...
okay. i figured it out. the problem was actually what i was passing into the functions:
cmStatus_01 was NOT the actual ID of the div. it was evaluated earlier like so:
var cmStatus_01 = document.getElementById('divName');
but i WAS passing the divName into the replaceImage() function and it was expecting the evaluted version of it, like cmStatus_01. so it broke.
so when i actually retooled the function so i was ONLY passing divName, it obviously worked. this is the retool:
function replaceNodeText(id, newText)
{
var node = document.getElementById(id);
while (node.firstChild)
{
node.removeChild(node.firstChild);
}
node.appendChild(document.createTextNode(newText));
}
project deadline too tight! it's making my brain fail. :P
also: apologies for not posting where the variables came from. that would have helped enormously, i'm sure and i don't know why i didn't think to post them as well.
thank you for your patience and your time.
WR!
So, here's the deal, I'm trying to use Element.previous to call an a previous element as an object... but it just gets the text of the element. How can I call on a previous object without knowing what it's id is? Calling on a previous element by class for example?
So, I figured it out partly: $('1').parentNode.previousSibling.firstChild calls the previous object.
Now, I'm having trouble getting it to call the previous object if an object has been deleted between them. Here's the script.
<script src="lib/prototype.js" type="text/javascript"></script>
<script type="text/javascript">
function printBr(element, index, array) {
document.write("<div class=\"row\">");
document.write("<input type=\"text\" class=\"textfield\" onclick=\"$('a" + index + "').appear(); return false;\" id=\"" + index + "\" name=\"" + index + "\" value=\"" + element + "\"><br/>");
if (index == 0) {}
else {
document.write("<div id=\"a" + index + "\" style=\"display:none;width:150px;height:25px;background-color:blue;\">");
document.write("<a onmousedown=\"$('" + index + "').parentNode.previousSibling.firstChild.writeAttribute('value', $('" + index + "').parentNode.previousSibling.firstChild.value + $('" + index + "').value);$('" + index + "').remove();$('a" + index + "').hide(); return false;\"><< Move</a> | ");
document.write("<a onmousedown=\"$('a" + index + "').hide(); return false;\">X</a></div>");
}
document.write("</div>");
}
['hello, world','123','bye bye birdy'].forEach(printBr);
</script>
Use .previous with the CSS selector as a argument to .previous. Such as .previous('span'). Or this element you are looking for may not be truly the previous element. It may be that you need to go up() first.