0

I am attempting to add the action attribute to my form element, on the load of a jquery function. However, I seem to be missing something. I have tried searching for this issue, and I have not found any luck in regards to my issue. Here is the HTML in question:

 <form method="post" enctype="multipart/form-data" id="#photouploader">
            Upload Client Photo Here
            <input type="file" name="thumbnail">
            <input type="submit" value="Upload Image">
          </form>

This is the Jquery function I wrote:

 $(document).ready(function(){
 var id = document.location.href.split('client/')[1]; 
 $('#photouploader').attr('action', '/form-upload/'+id);
 });

I am just looking to understand where I am going wrong here.

1
  • 5
    remove the # from the id attribute in the HTML Commented Aug 5, 2016 at 17:32

3 Answers 3

2

Your selector $('#photouploader').attr('action', '/form-upload/'+id); is looking for an id called photouploader. You do not have an HTML element with an ID of photouploader. Instead, you have an element with an ID of #photouploader.

Simply remove the # from your ID attribute in the HTML and you should be good to go -

<form method="post" enctype="multipart/form-data" id="photouploader">
Sign up to request clarification or add additional context in comments.

1 Comment

@mplungjan Quite honestly, because after posting my answer, the user commented on mine saying I was "stealing his rep" - He then posted his own answer 2 minutes AFTER that comment, leading me to believe it was knowingly submitted as a duplicate, not simultaneously. I've removed my downvote.
2

You have a # in your HTML form ID - Remove it and your jQuery should work. Should look like this:

<form method="post" enctype="multipart/form-data" id="photouploader">

The # in jquery is not part of the ID, it just indicates that the following text is referencing an element ID, similar to how . indicates class.

EDIT: Credit to eatpeanutbutter, he had the correct answer in the comments originally. I was typing mine up as it was posted.

5 Comments

@EatPeanutButter I was typing my answer up as you commented so I did not see yours before adding mine, AND I cited you in my answer once I realized. That being said, if you had the answer, why not post it AS an answer? Additionally, you actually submitted an answer AFTER my answer, so who's hijacking rep again?
You do not get rep from comments. In this case, the question was not even worth answering. Question has been close-marked as typo. Down-voter should remove downvote. Simultaneous answering can happen
I have removed the # in front of id. However, my method still seems not to work.
@tetraphenom I've tried your code above in this JSFiddle: jsfiddle.net/uxoxL7js and after doing an inspect element, it is definitely adding the action. Are you sure that your URL has the string 'client/' in it? If you check in console, are you getting any errors?
Regarding the URL, yes -- I am successfully running other code using same variable with same definition. Also, I am not getting any errors. However, once I check the html after compiling, the action does not appear in the form.
0

Remove # from id (id="photouploader">) and try this:

$(document).ready(function(){
 var id = document.location.href.split('client/')[1]; 
 $('#photouploader').attr('action', '/form-upload/'+id);
});

2 Comments

I have tried this. After looking in inspect element, I still could not find action embedded into the form element
I change a response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.