Solved

prepending a variable in javascript

Posted on 2007-04-06
10
1,506 Views
Last Modified: 2012-05-05
Is there a function out there that will do this:

somevar="something"+somevar;

such as .prepend()? if there isnt, how would imake this function work like this:

somevar.prepend(),

NOT like this though:

prepend(somevar, prepend_value);


If possible, I want both answers so if i ever need to i can alter variables with functions without putting the variable i want to alter in the argument list.
0
Comment
Question by:Oscurochu
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18869564
see JavaScripts prototype syntax and JSON http://www.json.org/
0
 
LVL 15

Expert Comment

by:stanscott2
ID: 18869679
Check out the use of the EVAL function.  This function takes a string and executes it, which I believe is what you're looking for.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 18877106
Why over-complicate things? What would this function gain you?

Obviously you know the value to prepend; why not just do as you suggested:

somevar="something"+somevar;
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Author Comment

by:Oscurochu
ID: 18909630
using a function to prepend a variable will clear up a lot of code. i like to keep my code clean, so:


document.getElementById('blah').innerHTML="blah blah"+document.getElementById('blah').innerHTML


makes the code look cluttered. it would be easier to just use:

document.getElementById('blah').innerHTML.prepend('blah blah');
0
 
LVL 15

Expert Comment

by:stanscott2
ID: 18909647
In the example you just gave, you really want to insert HTML, so use this function with two argument, the node to be acted on, and the text to be added:

prepend('myNode','blah blah');

function prepend(nd, txt) {
     var curNode = document.getElementById(nd);
     curNode.insertBefore(document.createTextNode(txt),curNode.firstChild);
}

If you aren't sure that there will always be a firstChild node, then modify the function like this:

function prepend(nd, txt) {
     var curNode = document.getElementById(nd);
     if (curNode.firstChild) {
          curNode.insertBefore(document.createTextNode(txt),curNode.firstChild);
     } else {
          curNode.appendChild(document.createTextNode(txt));
     }
}
0
 
LVL 5

Author Comment

by:Oscurochu
ID: 18909706
i already said, i donot want it to work like that because of specific reasons i cant remember right at this moment. im trying to get it to work  like this, if possible:

document.getElementById('blah').innerHTML.prepend('some text');

innerHTML is just an example. if the above function is not possible, then i will just write a function that will use two arguments to do the work.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18910306
as I already said: JSON can do that
0
 
LVL 29

Expert Comment

by:Badotz
ID: 18910802
<$.02>Much ado about nothing</$.02>
0
 
LVL 5

Author Comment

by:Oscurochu
ID: 18911968
i dont understand how JSON works, can you help me out with that?
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 20 total points
ID: 18956414
see prototype.js, something like:

String.extend(String.prototype, {prepend: function(str){ /* work here */ });

€.02 as asked for ;-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question