JavaScript - String Compares
When comparing an empty string "" to another string like "1232", javascript is returning true. Why is that?
Similar TutorialsHey all, I have a simple example below showing how when I pass in the value of the value attribute of option node, and then use if operator to check whether parameter is a string or not, even though it's a string, it converts it to false boolean and triggers the else statement rather than calling a function: Code: <body> <select> <option value="createMsg">Add Message</option> <option value="removeMsg">Remove Message</option> </select> </body> Code: var menu = { handleMenu : function(callback){ if(callback === "string"){ menu[callback](); } else { console.log("Issue occurred") } }, createMsg : function(){ var content = document.createTextNode("Please give additional information."), heading = document.createElement("h1"); heading.appendChild(content); document.body.appendChild(heading); }, removeMsg : function(){ } } document.getElementsByTagName('select')[0].onchange = function(){ menu.handleMenu(this.value)}; callback should be a string so why is it saying otherwise? Thanks for response I need help with the following code below. It just doesn't turn out okay. Any ideas? Code: function changing() { A = document.exempel.Amount.value B = ("<%#Container.DataItem("Price_2_qty")%>") C = ("<%#Container.DataItem("Price_3_qty")%>") D = ("<%#Container.DataItem("Price_4_qty")%>") E = ("<%#Container.DataItem("Price_5_qty")%>") if (A < B) {document.exempel.Price.value = "<%#Container.DataItem("Price_1")%>"} else if (A = B && (!(A >= C))) {document.exempel.Price.value = "<%#Container.DataItem("Price_2")%>"} else if (A = C && (!(A >= D))) {document.exempel.Price.value = "<%#Container.DataItem("Price_3")%>"} else if (A = D && (!(A >= E))) {document.exempel.Price.value = "<%#Container.DataItem("Price_4")%>"} else if (A >= E) {document.exempel.Price.value = "<%#Container.DataItem("Price_5")%>"} } I was looking at this page, and came across "%" used in strange context. It appears that the mod operator can be used in Strings?? http://download.oracle.com/javase/tu...vaOO/enum.html What does this do/ mean? Thanks!!: System.out.printf("Your weight on %s is %f%n", p, p.surfaceWeight(mass)); I am trying to use some string functions with no results. It appear what I'm using them on are not strings. my_string = document.getElementById("link5").onclick; document.write(my_string.lastIndexOf("5")); This gets nothing! And document.write(document.getElementById("link5").onclick.lastIndexOf("5")); gets nothing. BUT if I make a string and put what the onclick is set to with: my_string = "function onclick(event) { reveal(\"5\", \"2\", \"1864\")"; then my_string.lastIndexOf("5") will yield 34. So why is my original my_string = not working? And how can I get it to work? This is probably easy if I knew Javascript. Can someone show me the code to get the number "800014352" part from the url???? http://testebiz/ebizbtest/ShoppingCa...2/Default.aspx Hey guys. I'm trying to display a grid of data basically, but inside a table and with appropriate closing and beginning of td and tr tags. I'm having a very weird problem. Here is my code: Code: tasks = document.getElementById('tasks'); var i = 0; tasks.innerHTML = <tablebgcolor=silver>"; while (i < 3) { tasks.innerHTML = tasks.innerHTML + "<tr><td>"; tasks.innerHTML = tasks.innerHTML + text[i][0]; tasks.innerHTML = tasks.innerHTML + "</td><td>"; tasks.innerHTML = tasks.innerHTML + text[i][1]; tasks.innerHTML = tasks.innerHTML + "</td><td>"; tasks.innerHTML = tasks.innerHTML + text[i][2]; tasks.innerHTML = tasks.innerHTML + "</td></tr>"; i++; } tasks.innerHTML = tasks.innerHTML + "</table>"; The html just doesn't work when it's in a string. I have to put each "<tr><td>", "</td><td>", </td></tr>", <table bgcolor=silver>", etc. into it's own variable and do it that way. That doesn't seem like it should be happening and I don't think it would be good to do it that way in the long run. Can someone tell me what I'm doing wrong? Hi guys im new here i have a project about searching string im am going to use javascript the program should look like this. Enter first word: abcd Search in first Word: ab then pop up ab found then if i type like sdg then pop up sdg not found something like this plss how can i do that Help, I can't figure out why this doesn't work. Thanks.. [CODE] 1 //==UserScript== 2 // @name Add youcalc highrise tab 3 // @namespace http://www.youcalc.com/ 4 // @include http://*.highrisehq.com/* 5 // @include https://*.highrisehq.com/* 6 // @exclude http://www.highrisehq.com/* 7 // @exclude http://forum.highrisehq.com/* 8 // @exclude https://www.highrisehq.com/* 9 // ==/UserScript== 10 11 //change to your needs 12 13 appurl1 = "http://www.youcalc.com/pubapps/1232017802730/"; 14 tabtitle1 = "Deals and forecast"; 15 appwidth1 = "830px"; 16 appheight1 = "690px"; 17 18 appurl2 = "http://www.youcalc.com/pubapps/1226931095311/"; 19 tabtitle2 = "Deals Activity Report"; 20 appwidth2 = "830px"; 21 appheight2 = "690px"; 22 23 appurl3 = "http://www.youcalc.com/pubapps/1229432659119/"; 24 tabtitle3 = "Contacts By Tag"; 25 appwidth3 = "830px"; 26 appheight3 = "690px"; 27 28 //from here, change at own risk 29 tabs = document.getElementById('Tabs'); 30 tabshtml = tabs.innerHTML; 31 32 //new tab html and JS 33 youcalcapp1 = '<iframe src=' + appurl1 + ' width=' + appwidth1 + ' height=' + appheight1 + ' style=border:none; />'; 34 youcalcapp2 = '<iframe src=' + appurl2 + ' width=' + appwidth2 + ' height=' + appheight2 + ' style=border:none; />'; 35 youcalcapp3 = '<iframe src=' + appurl3 + ' width=' + appwidth3 + ' height=' + appheight3 + ' style=border:none; />'; 36 37 putyoucalc1 = "inner_body = document.getElementById('screen_body');inner_body.innerHTML = '"+ youcalcapp1 + "';yct1 = document.getElementById('youcalctab1');yct1.className='current';yct2 = document.getElementById('youcalctab2');yct2.className='';";yct3 = document.getElementById('youcalctab3');yct3.className='';"; =========================================================^ SyntaxError: unterminated string literal 38 putyoucalc2 = "inner_body = document.getElementById('screen_body');inner_body.innerHTML = '"+ youcalcapp2 + "';yct2 = document.getElementById('youcalctab2');yct2.className='current';yct3 = document.getElementById('youcalctab3');yct3.className='';";yct1 = document.getElementById('youcalctab1');yct1.className='';"; 39 putyoucalc3 = "inner_body = document.getElementById('screen_body');inner_body.innerHTML = '"+ youcalcapp3 + "';yct3 = document.getElementById('youcalctab3');yct3.className='current';yct2 = document.getElementById('youcalctab2');yct2.className='';";yct1 = document.getElementById('youcalctab1');yct1.className='';"; 40 41 //find last tab, and put youcalc tab after 42 lasttab = 'Tags</a></li>'; 43 youcalctabs = '<li class="with_space" ><a class="" id="youcalctab1" onclick="' + putyoucalc1 + '" href="#">' + tabtitle1 + '</a></li><li class="with_space" ><a class="" id="youcalctab2" onclick="' + putyoucalc2 + '" href="#">' + tabtitle2 + '</a></li>';<li class="with_space" ><a class="" id="youcalctab3" onclick="' + putyoucalc3 + '" href="#">' + tabtitle3 + '</a></li>'; 44 tabshtml = tabshtml.replace(lasttab,lasttab+youcalctabs); 45 tabs.innerHTML = tabshtml; I am using javascript to search for the '*' character in a string (ie. 'username*:') but using search('\*') does not work. How do I search for it since the search function takes a regexp argument and * messes that up?
Hi, I'm working on refining my search function. At the moment it works fine like this: Code: function searchLocations() { var found = false; var input = document.getElementById('autocomplete').value; for (var j = 0; j < gmarkers.length; j++) { if (gmarkers[j].myname.toLowerCase() == input.toLowerCase()) { found = true; gmarkers[j].show(); myclick(j); } } if ( ! found ) alert("No matches found. Please check your spelling or refine your search."); } but you have to get the input exactly the same as it is in the array for it to work (as it should, being that that's the way it's written). I wanted to split it so that for example Black Cat Hotel would pop if the user entered Black OR Cat OR Hotel (or any combination of the above). So I thought that this might work: Code: function searchLocations() { var found = false; var input = document.getElementById('autocomplete').value; for (var j = 0; j < gmarkers.length; j++) { var s = gmarkers[j].myname.toLowerCase().split(" "); if ( s == input.toLowerCase()) { found = true; gmarkers[j].show(); myclick(j); } } if ( ! found ) alert("No matches found. Please check your spelling or refine your search."); } but obviously not. Is there some better way to do this? Hello, I need your help. For one reason or another, I have a textarea with text in it, however, when I call it back and store it in var, it seems to strip all the spaces. [ID = "COMMENTS"] Ie. of what's in the textarea: ---------------------------------------------- Apples are red Oranges are orange Pears are green Bananas are yellow var comments = document.getElementById("comments").value returns: Apples are red Oranges are orange Pears are green Bananas are yellow How could this be re-written such that the spaces in between the text aren't ignored? Much thanks and appreciation for everyones help. Cheers J How to handle String in Javascript
Hello, I have the following Javascript function: Code: function loadReviews(){ var url='http://search.twitter.com/search.json?callback=?&q=test'; jQuery.getJSON(url,function(json){ jQuery.each(json.results,function(i,review){ jQuery("#divroller_container").append('<div class="reviews" id=item'+i+'>'+review.text+'<br>Reviewed By: '+review.from_user+'</div>'); }); start(30000, 1); }); } I'm trying to use this in a PHP page. I tried to make do an echo ' and then display the Javascript but I'm struggling on how to convert the following line to PHP to that it still works like it should in Javascript: Code: jQuery("#divroller_container").append('<div class="reviews" id=item'+i+'>'+review.text+'<br>Reviewed By: '+review.from_user+'</div>'); I still need the Javascript variables to function the same etc. Any help would be greatly appreciated. Thanks in advance! My problem is relates to getting part of the text from a div, then pass this on to a variable which then acts upon it. This is a short summary in detail: http://jsfiddle.net/defencedog/W9Fsw/ Above simulates a Forum like environment (PHPBB3). here user can't insert html in posts thus a customised BBcode ([doc])is made in which is placed the required src attribute, unique to every SCRIBD document. The goal is to get the text within the two [doc] & that will be something like (discarding the two BBCodes) Code: src="http://www.scribd.com/embeds/2942002/content?start_page=1&view_mode=list&access_key=key-sxlnrfvqu5s22kzi5xb" then pass it on as a variable named src, which will then replace the contents to achieve desired output. Notice the two problems 1] how do I select the div upon which the action is to take place I.e that contains [doc]. Remember there occurs other divs as well in a posted message? 2] How do I select the desired text? Plz do giv me some suggestions Hello, I need your help. Let's say for example if I have the following file number: 2011-1234567 and if the value of the variable is already the same (ie. var fileNo = 2011-1234567) how can I go about automatically adding a number at the end? ex.1.) var fileNo = "2011-1234567" if (fileNo already exists) { fileNo = 2011-1234567-2 } ex.2.) var fileNo = "2011-1234567-2" if (fileNo already exists) { fileNo = 2011-1234567-3 } ect. Much thanks and appreciation for all your help. Jay Hello, I need your help. How could I write a functional if statement such that it would check against a series of various word strings seperated by commas ie. function check(y) { //y = "FOR INPUT" var string_to_check = FOR INPUT, FOR ACTION, FOR APPROVAL if (y isin string_to_check) { return 1 } } Thanks for everyones help. J I'm trying to determine whether a word exists in the URL and if so, change the CSS of a div. I know how to do each task individually, but combining the two is giving me trouble. So far I have: Code: function checkSent(divname) { if(document.URL.indexOf("sent") >= 0) *document.getElementById(success).style.display = 'block'; } var sentornot = checkSent(); // If the URL contains the word, sent, then change the display of the element "success" to block I can successfully determine whether the string exists in the URL by adding an alert, but the Javascript to change the CSS doesn't work. The CSS for the div element "success" is as follows: Code: #success { display: none; } Any help is appreciated! Thanks! Edit: If needed, this is the code I used to test whether the "check URL" portion works: Code: function checkSent(divname) { if(document.URL.indexOf("sent") >= 0) { alert("found"); document.getElementById("success").style.display = 'block'; //this line doesn't work :( } else{ alert("not found"); } } What would the best be to split the following string: "only(a-z,A-Z,1-9)" I want to split it into a array to look like this: array(only, a-z, A-Z, 1-9); Hey guys, I need some help with fetching a string to be the button text from the html, instead of building it in the javascript, so I can localize it for translations. Right now I have this generating the button: Code: <input type="button" id="share_validate_button" name="share_validate_button" value="<?php echo __('Test Share'); ?> " /> The function __() I am using for the localization stuff, in case you were wondering what that was for. Anyways, below is the JS associated with this button Code: function share_validate_click_add () { $('#share_validate_button').removeAttr('disabled').attr('value', 'Test Share').one('click', function(){ xajax_settings_osb_data_windows_verify(xajax.getFormValues('data_info_form')); $('#share_validate_status').html(''); $(this).attr('disabled', 'disabled').attr('value', 'Testing Share...'); }); } This will display the correct value for the button, and on click, the button disables and text changes to 'Testing Share...' The only problem is the JS overrides the value I set in the html, so I can't localize it. I was wondering if there was a way for me to set the two values (Test Share & Testing Share...) in the html above where we set id and class, and just fetch them in the js where needed. Is this possible? If someone can help me I would really appreciate it! Thanks! |