0

I have the following code:

$('input.next_page_img').replaceWith('<input type="submit" class="next_page_image graybutton mediumbutton" alt="Next Page" value="Next Page" title="Next Page" onclick="Add_Search_Param('page', 2); Refine();">');

The problem is with the onclick function. It makes the rest of the script fail. When I take out the onclick function, it works. What do I need to do to fix this?

Scratch that, I've ultimately decided this was a bad idea. Instead I'd rather set the attributes of the inputs... why isn't this working though http://jsfiddle.net/qSBHH/ ?

1
  • 2
    You have a quotation mark problem. I suggest to create the element and to attach the event handler with jQuery. Commented Dec 18, 2011 at 0:19

3 Answers 3

2
bookmarker = $('<span />').insertBefore('.next_page_img');
$('.next_page_img').detach().attr({type: 'submit', value: 'save'}).insertAfter(bookmarker);
Sign up to request clarification or add additional context in comments.

2 Comments

I've ultimately decided this was a bad idea. Instead I'd rather set the attributes of the inputs... why isn't this working though jsfiddle.net/qSBHH ?
I understand... I tried to implement it into my site, it resulted in two save buttons. But also, I reviewed the script and decided it was too "hack"-y for my taste. I replace the src attribute with an all text image and style the button, it came exactly how I wanted it to other than I wanted it to be text. Thank you though. Your first solution answered my initial question so I'll select your answer as correct.
0

Since you're using jQuery anyway, it'd be a lot less messy to add the event handler with the library:

$('input.next_page_img')
  .replaceWith($('<input type="submit"/>', {
    'class': "next_page_image graybutton mediumbutton",
    'alt': "Next Page",
    'value': "Next Page",
    'title': "Next Page",
    'click': function() {
      Add_Search_Param('page', 2);
      Refine();
    }
  }));

You can jam all that on one line if you like.

6 Comments

I've ultimately decided this was a bad idea. Instead I'd rather set the attributes of the inputs... why isn't this working though jsfiddle.net/qSBHH ?
What exactly makes it a "bad idea"? It's much easier to maintain; having to quote raw JavaScript code with strings in is pretty ugly. edit oh wait -- you mean that what you were doing in the first place was a bad idea? Sorry I'm confused now :-)
@Pointy, how can "click" work there. It should be same as $(elem).attr("click", function(){}) which doesn't make any sense. Am I doin' it wrong?
My whole entire idea was bad, since the onclick event changes every next page you go to...
@Esailija Note that I wrapped the <input> in its own jQuery call. When you're calling jQuery to create a node, you can pass a second object argument with attributes to set. The library knows to look for the handler names there. It also recognizes "css" and checks if its value is an object.
|
0

Why don't you just do this:

$('input.next_page_img').replaceWith($("<input>", {
    type: "submit",
   "class": "next_page_image graybutton mediumbutton",
    alt: "Next Page",
    value: "Next Page",
    title: "Next Page"
}).bind("click", function() {
    Add_Search_Param('page', 2);
    Refine();
}));

1 Comment

I've ultimately decided this was a bad idea. Instead I'd rather set the attributes of the inputs... why isn't this working though jsfiddle.net/qSBHH ?

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.