I'm a bit new at Javascript... What I need to do is get a variable by it's name, which the code can get as a string, but that string is going to be different depending on input etc. My searching hasn't gotten anything so far, but maybe I don't know the correct terms to use to describe this. This is what I made so far:
var 1name = 'test'; // This part is added by php, and
var 1data = 'THIS IS TEST TABLE RAWR'; // the variable names end up being
var 2name = 'etc'; // a number followed by "name", etc.
var 2data = 'MORE PLACEHOLDER TEXT HERE';
later on:
function editTable(id) {
if (id != null) {
document.getElementById('name').value = window[id + "name"];
}
}
and a button:
<input type='button' value='Edit' onclick="editTable(document.getElementById('table').value)" />
The button grabs the value of a form field, which will match the number of one set of the variables added from the php. How can I get this to work? I've already made sure the variables are getting inserted into the page, and that the button gets a value that matches.
Can you see what I was going for or do you need more explanation?
Variables cannot start with numbers. Personally I think you should use an array like this:
var data = [
{name:"text",data:"THIS IS TEST TABLE RAWR"},
{name:"etc",data:"MORE PLACEHOLDER TEXT HERE"}
];
Since you've mentioned PHP outputting this, look at json_encode. Then you could just have:
var data = <?php echo json_encode($data); ?>;
Assuming, of course, that $data is your array structure.
Related
I have a series of selector fields in a form. (The values of each selector are passed in to variables.) Once all fields are selected the submit button sends the user to the page with products sorted by a single category and multiple product characteristics using the www.mydomain.com/?product_cat=cat-name&characteristics=foo,bar,candy URL query.
The query works when I type it into the browser. When I try passing it through using jQuery it only passes the question mark (www.mydomain.com/?). If I remove the question mark then the rest of the string passes through. Am I using the wrong method? Here's my code:
jQuery(function($) {
$("#msform").submit(function() {
var homeType = $("#homeType").val(); //drop down selector value builds 1st part of category name
var eqType = $("#eqType").val(); // drop down selector value builds last part of category name
var sqType = $("#sqType").val(); // product characteristic value from drop down selector
var flowType = $("#flowType").val(); // prod char value
var locationType = $("#locationType").val(); //prod char value
var urlSet = "/?product_cat="+homeType+"-"+eqType+"&characteristics="+sqType+","+flowType+","+locationType; // URL I wish to send the visitor to after clicking submit button
$("#msform").attr('action', urlSet);
$("#msform").submit();
});
});
I have tried using ascii codes for the question mark. I have escaped the question mark but nothing seems to allow the whole string to pass to the URL.
below code is just a try, Its better to do ajax form submission
jQuery(function($) {
homeType = $("#homeType").val();
eqType = $("#eqType").val();
sqType = $("#sqType").val();
flowType = $("#flowType").val();
locationType = $("#locationType").val();
urlSet = "<?php echo home_url();?>/?product_cat="+homeType+"-"+eqType+"&characteristics="+sqType+","+flowType+","+locationType;
$("#msform").attr('action', urlSet);
$("#msform").submit(function() {console.log('submit success')});
});
Hey i want to store a piece of text plus a variable in localstorage.
This may seem a bit vague so here is what i mean with this:
var something = "text";
localStorage.setItem("localstorage", '"sometext"+something');
function functionz()
{
var displayed = localStorage.localstorage;
alert(displayed);
}
I want to make sure that displayed will display this text: sometexttext
I had it already working by storing "sometext"+something in a variable like this:
var something = "text";
localStorage.setItem("localstorage", '"sometext"+something');
function functionb()
{
var thetext = "sometext"+something;
alert(thetext);
}
The second version will give you: (sometexttext), and the first one will give you ("sometext"+something) as raw text. This is ofcourse not what i want and i wonder how i can make it so that it can be stored in localstorage and still display the second result.
Thanks in advance
Say I have variables that I acquire in one html page, such as a UserName or a url or something. And in another html page I have input boxes for these variables and I want to autocomplete them by sending the data from the first html page to the input boxes in the second one. Can anyone indicate to me how I can achieve this?
Use JavaScript to create the equivalent collection for use by other JS code:
Code:
<script type="text/javascript">
var querystring = [ ];
var qs = location.search;
if ( qs.length > 1 )
{
qs = qs.substring(1); // skip past the ?
var pairs = qs.split( /\&/g ); // get all the name=value pairst
for ( var p = 0; p < pairs.length )
{
var pair = pairs[p];
querystring[ pair[0] ] = unescape( pair[1].replace(/\+/g," ");
}
}
</script>
Then, anyplace in your page where in ASP code you might use
Code:
var foo = Request.QueryString("foo");
you instead simply do
Code:
var foo = querystring["foo"];
CAUTION: "foo" will be case sensitive, unlike in ASP. If you wish, you could replace
Code:
querystring[ pair[0] ] = unescape( pair[1].replace(/\+/g," ");
with
querystring[ pair[0].toLowerCase() ] = unescape( pair[1].replace(/\+/g," ");
and then always use lower case names ("foo" in place of "Foo" or "FOO") when finding values.
Untested, though I have used this same code before. If there's a goof, it's just a typo.
You can use JQuery for that. Assuming you are not using any server side code.
Pass the value as a param to the next page.
Like myurl?param=xyz
Then you can get the value in the next page like this,
See this answer and sourcecode
var xyz = jQuery.url.param("param_in_url");
For this you can use php session .store that variable in session and get them in any page.or if you are calling that page from the page where u have values say username call like
next page
You should use $_SESSION variable in php. OR you can use sessionStorage of javascript.
I am trying to write something that would look at tweets and pull up info about stocks being mentioned in the tweet. People use $ to reference stock symbols on twitter but I cant escape the $.
I also dont want to match any price mention or anything like that so basically match $AAPL and not $1500
I was thinking it would be something like this
\b\$[a-zA-Z].*\b
if there are multiple matches id like to loop through them somehow so something like
while ((tweet = reg.exec(sym_pat)) !== null) {
//replace text with stock data.
}
This expression gives me an unexpected illegal token error
var symbol_pat = new RegExp(\b\$[a-z]*);
Thanks for the help if you want to see the next issue I ran into
Javascript AJAX scope inside of $.each Scope
Okay, you've stated that you want to replace the matches with their actual stock values. So, you need to get all of the matching elements (stock ticker names) and then for each match you're going to replace the it with the stock value.
The answer will "read" very similarly to that sentence.
Assume there's a tweet variable that is the contents of a particular tweet you're going to work on:
tweet.match(/\b\$[A-Za-z]+\b/g).forEach(function(match) {
// match looks like '$AAPL'
var tickerValue = lookUpTickerValue(match);
tweet.replace(match, tickerValue);
});
This is assuming you have some logic somewhere that will grab the ticker value for the given stock name and then replace it (it should probably return the original value if it can't find a match, so you don't mangle lovely tweets like "Barbara Streisand is $ATAN").
var symbol_pat = new RegExp('\\b\\$[a-z]+\\b','gi');
// or
var symbol_pat = /\b\$[a-z]+\b/gi;
Also, for some reason JS can not calculate the beginning of a word by \b, it just catches the one at the end.
EDIT: If you're replacing the stock symbols you can use the basic replace method by a function and replace that data with predefined values:
var symbol_pat = /(^|\s)(\$[a-z]+\b)/gi;
var stocks = {AAPL:1,ETC:2}
var str = '$aapl ssd $a a$s$etc $etc';
console.log(str);
str = str.replace(symbol_pat, function() {
var stk = arguments[2].substr(1).toUpperCase();
// assuming you want to replace $etc as well as $ETC by using
// the .toUpperCase() method
if (!stocks[stk]) return arguments[0];
return arguments[0].replace(arguments[2],stocks[stk]);
});
console.log(str);
I have 3 HTML form inputs fields that is dynamically generated by a "add more" button, with naming for the fields name as fieldName, fieldName1, fieldName2, fieldName3, and so on.
Now, I'm trying to retrieve the value from this fields with JavaScript, using the script below.
var bookingForm = document.forms['formName'];
var qty = bookingForm.fieldName +'i'.value;
with the 'i' been a generated numeric number by a for loop
when I use alert(qty), it returns NaN, when I'm expecting the value for fieldName1, fieldName2, and so on.
But when I use;
var qty = bookingForm.fieldName.value
I can get the value in that field but get NaN when I try to concatenate 1,2,3, with the fieldName.
Any help will be very much appreciated.
You use brackets to access a property using a string:
var qty = bookingForm['fieldName' + i].value;
You can't use code like:
var qty = bookingForm.fieldName +'i'.value;
bookingForm.fieldName +'i' is a string. You have to change that string into a DOM element in order to access the .value parameter.
Try document.getElementsByName('fieldName'+i)[0].value