views:

40

answers:

2
var tagMatch;
    if (tagMatch = window.location.href.match(/\/questions\/ask\?tags=([^&]+)/)) {
       $(function() { 
         if (!$('#tagnames').val().length) {
         $('#tagnames').val(unescape(match[1].replace(/\+/g, ' '));
          }
     });
     }

Hi all, this JS code is supposed to match the latter-part of a URL of the form /questions/ask?tags=some-tag, and then plug the text contained in the part of the URL after tags= into a textbox with the id #tagnames. What am I doing wrong? How can I fix this?

I'm still learning so if you would want to show me how to fix my regex or anything else, please do!

+1  A: 

Without looking too much into how the string matching works...

You seem to be defining and setting a variable called tagMatch, but then you're using a variable called match to set the value.

Is that the problem?

Update: Apologies - your regex is correct - I misread the intention :)

Damovisa
hmm...still not formfilling. I wonder why?
Alex
Does it work if you have a second querystring variable? Without one?
Damovisa
+1  A: 

I think the error is with this line

if (!$('#tagnames').val().length)

length will return a number and check that against number.

Something like

if ($('#tagnames').val().length > 0)

I don't think there is a need to place document ready inside the if statement. Isn't this better.

 $(function() { 
       var tagMatch;
       if (tagMatch = window.location.href.match(/\/questions\/ask\?tags=([^&]+)/)) 
       {
           if ($('#tagnames').val().length > 0)
           {
               $('#tagnames').val(unescape(match[1].replace(/\+/g, ' '));
           }
       }
    });
rahul
Yea, if #tagnames is valid, `$('#tagnames').val().length` always return >= 0 but I'm not sure this is the ONLY problem here.
o.k.w
tagmatch vs. match was wrong. That was it. TY!
Alex
Hey Alex, not meaning to be picky, but if it was the tagMatch vs match variable name... shouldn't mine be the "correct" answer? :)
Damovisa
Yes, and I thank you for that, but rahul put more effort into his answer.
Alex